Tim*_*och 104 javascript enums typescript
我收到一个号码type = 3,必须检查它是否存在于此枚举中:
export const MESSAGE_TYPE = {
INFO: 1,
SUCCESS: 2,
WARNING: 3,
ERROR: 4,
};
Run Code Online (Sandbox Code Playgroud)
我发现的最好方法是将所有枚举值作为数组并使用indexOf.但结果代码不是很清晰:
if( -1 < _.values( MESSAGE_TYPE ).indexOf( _.toInteger( type ) ) ) {
// do stuff ...
}
Run Code Online (Sandbox Code Playgroud)
有更简单的方法吗?
Xiv*_*Xiv 126
如果你想使用字符串枚举,你需要使用Object.values(ENUM).includes(ENUM.value)因为字符串枚举不是反向映射的,根据https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-4.html:
Enum Vehicle {
Car = 'car',
Bike = 'bike',
Truck = 'truck'
}
Run Code Online (Sandbox Code Playgroud)
变为:
{
Car: 'car',
Bike: 'bike',
Truck: 'truck'
}
Run Code Online (Sandbox Code Playgroud)
所以你只需要这样做:
if (Object.values(Vehicle).includes(Vehicle.car)) {
// Do stuff here
}
Run Code Online (Sandbox Code Playgroud)
如果您收到错误:Property 'values' does not exist on type 'ObjectConstructor',那么您的目标不是ES2017.您可以使用此tsconfig.json配置:
"compilerOptions": {
"lib": ["es2017"]
}
Run Code Online (Sandbox Code Playgroud)
或者你可以做任何演员:
if ((<any>Object).values(Vehicle).includes(Vehicle.car)) {
// Do stuff here
}
Run Code Online (Sandbox Code Playgroud)
Sar*_*ana 113
如果您使用的是TypeScript,则可以使用实际的枚举.然后你可以使用它来检查它in.请注意,这仅适用于您的枚举不是const且基于数字的情况:
export enum MESSAGE_TYPE {
INFO = 1,
SUCCESS = 2,
WARNING = 3,
ERROR = 4,
};
var type = 3;
if (type in MESSAGE_TYPE) {
}
Run Code Online (Sandbox Code Playgroud)
这是有效的,因为当您编译上面的枚举时,它会生成以下对象:
{
'1': 'INFO',
'2': 'SUCCESS',
'3': 'WARNING',
'4': 'ERROR',
INFO: 1,
SUCCESS: 2,
WARNING: 3,
ERROR: 4
}
Run Code Online (Sandbox Code Playgroud)
Soo*_*ead 35
根据 Sandersn 的说法,最好的方法是:
Object.values(MESSAGE_TYPE).includes(type as MESSAGE_TYPE)
Run Code Online (Sandbox Code Playgroud)
Jay*_*tin 29
export enum YourEnum {
enum1 = 'enum1',
enum2 = 'enum2',
enum3 = 'enum3',
}
const status = 'enumnumnum';
if (!Object.values(YourEnum).includes(status)) {
throw new UnprocessableEntityResponse('Invalid enum val');
}
Run Code Online (Sandbox Code Playgroud)
小智 13
您的问题有一个非常简单的解决方案:
var districtId = 210;
if (DistrictsEnum[districtId] != null) {
// Returns 'undefined' if the districtId not exists in the DistrictsEnum
model.handlingDistrictId = districtId;
}
Run Code Online (Sandbox Code Playgroud)
export enum UserLevel {
Staff = 0,
Leader,
Manager,
}
export enum Gender {
None = "none",
Male = "male",
Female = "female",
}
Run Code Online (Sandbox Code Playgroud)
日志中的差异结果:
log(Object.keys(Gender))
=>
[ 'None', 'Male', 'Female' ]
log(Object.keys(UserLevel))
=>
[ '0', '1', '2', 'Staff', 'Leader', 'Manager' ]
Run Code Online (Sandbox Code Playgroud)
解决方案,我们需要将键作为数字删除。
export class Util {
static existValueInEnum(type: any, value: any): boolean {
return Object.keys(type).filter(k => isNaN(Number(k))).filter(k => type[k] === value).length > 0;
}
}
Run Code Online (Sandbox Code Playgroud)
用法
// For string value
if (!Util.existValueInEnum(Gender, "XYZ")) {
//todo
}
//For number value, remember cast to Number using Number(val)
if (!Util.existValueInEnum(UserLevel, 0)) {
//todo
}
Run Code Online (Sandbox Code Playgroud)
小智 7
类型断言是不可避免的。跟进
enum Vehicle {
Car = 'car',
Bike = 'bike',
Truck = 'truck'
}
Run Code Online (Sandbox Code Playgroud)
我找到了一个没有提到的替代方案,所以我想我会分享我的修复方法:
const someString: Vehicle | string = 'car';
const inEnum = (Object.values(Vehicle) as string[]).includes(someString);
Run Code Online (Sandbox Code Playgroud)
我觉得这更真实,因为我们通常使用类型安全(带有字符串)并希望将其与枚举进行比较;将其类型转换为any(原因:永远不要这样做)或Vehicle(原因:可能不真实)有点鲁莽。相反,将Object.values()输出类型转换为字符串数组实际上非常真实。
| 归档时间: |
|
| 查看次数: |
64516 次 |
| 最近记录: |