Is it possible to have tslint type check enum types?

Scott Reed Source

Is it possible to have tslint throw an error when a variable is assigned a value that is incompatible with its enum type?

For example:

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
javascripttypescripttslint

Answers

comments powered by Disqus