[英]Can I define a typescript variable that is an enum and a function?
这是到目前为止的代码:
function fr(this: any, value: number) {
return Object.keys(this).find(k => this[k] === value);
}
type getEnumValue = (value: number) => string;
type myEnum = typeof enumVal;
enum enumVal {
a = 1,
b = 2
}
let EnumEval = fr.bind(enumVal) as getEnumValue | myEnum;
Object.assign(EnumEval, enumVal);
// Why are the casts required?
console.log((EnumEval as myEnum).a);
console.log((EnumEval as getEnumValue)(1));
console.log(EnumEval as myEnum.a);
console.log(EnumEval(1));
最后两行出现 TS 错误,需要我进行转换。 为什么?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.