在TypeScript中枚举

时间:2018-06-07 14:11:10

标签: typescript

我在使用Typescript中的枚举时遇到问题。

export enum RegistrationStatusEnum {
        Pending,
        Rejected,
        Approved,
        InManagement,
    }

UserDTO.ts

import { RegistrationStatusEnum } from "../../Enums/RegistrationStatusEnum";

export class UserDTO {
  Name: string
  RegistrationStatus: RegistrationStatusEnum
}

OtherTS.ts

disableStatus(currentUser: UserDTO):boolean
    return currentUser.RegistrationStatus.Pending || currentUser.RegistrationStatus.Rejected || currentUser.RegistrationStatus.InManagement;
}

当我尝试访问Pending或其他枚举时,我收到错误'RegistrationStatusEnum'类型中不存在'Pending'属性发生了什么? THX!

1 个答案:

答案 0 :(得分:2)

currentUser.RegistrationStatus是枚举值,它可以是枚举成员之一,但要检查是否需要使用===运算符并与枚举中的特定字段进行比较:

disableStatus(currentUser: UserDTO): boolean {
    return currentUser.RegistrationStatus === RegistrationStatusEnum.Pending 
        || currentUser.RegistrationStatus === RegistrationStatusEnum.Rejected 
        || currentUser.RegistrationStatus === RegistrationStatusEnum.InManagement;
}