是否可以使用tslint类型检查枚举类型?

时间:2018-06-13 21:08:48

标签: javascript typescript tslint

当为变量分配与其枚举类型不兼容的值时,是否可能让tslint抛出错误?

例如:

export enum Gender {
    male = 1,
    female = 2
}

export class User {
    id: number;
    gender: Gender;
}

var user = new User();

user.gender = Gender.Male; // OK

user.gender = 3; // Currently is OK, desired result: TYPE ERROR

if (user.gender === Gender.Male) {} // OK

if (user.gender === 3) {} // Currently is OK, desired result: TYPE ERROR

0 个答案:

没有答案