[英]Typescript enum generic mapping
Helo Stack Community, I have following enum
mapping class: Helo Stack社区,我有以下enum
映射类:
export class RestEnumMpapper<T> {
constructor() {}
getEnumAsString<T>(o: T, key: string | number): string {
if (typeof key === 'string') {
return (o as T)[(o as T)[key]];
} else if (typeof key === 'number') {
return (o as T)[key];
} else {
throw new Error(`Unable to parse enum from ${typeof(key)}`);
}
}
/* ... Rest of enum converting class code ... */
}
And typical use case is following: 典型的用例如下:
export class PositionEvent {
private static statusEnumMapper: RestEnumMpapper<EventState> = new RestEnumMpapper<EventState>();
/* ... */
this.status = PositionEvent.statusEnumMapper.getEnumAsString(EventState, iPos.status) as EventState;
}
It works pretty well, but I'm currently linting my code and linter complains about shadowing generic type T
in RestEnumMpapper
class here: 它工作得很好,但是我目前正在整理我的代码,而linter抱怨这里的RestEnumMpapper
类中的通用类型T
RestEnumMpapper
了:
export class RestEnumMpapper<T> {
and here: 和这里:
getEnumAsString<T>(o: T, key: string | number): string {
Which is rational and make sense to leave generic type as class type. 将通用类型保留为类类型是合理的并且有意义的。 However, when I'm dropping T
on function declaration on each call I'm getting following TypeScritp
error: 但是,当我在每次调用时在函数声明上放下T
时,都会收到以下TypeScritp
错误:
[ts] Argument of type 'typeof EventState' is not assignable to parameter of type 'EventState'. [ts]类型'EventState'的参数不能分配给'事件状态'的参数。
It can be forced to work by passing value of enum (or any other plain number) but function obviously fails when trying to resolve statement like 2[3]
. 可以通过传递枚举值(或任何其他普通数字)来强制工作,但是在尝试解析2[3]
类的语句时,函数显然会失败。
I'd be thankful for any suggestion how to fix this? 如果有任何建议解决此问题,我将不胜感激。 Before dropping generic type in function TypeScritp was somehow able to resolve T as object with properties and just a name of enum was sufficient to work. 在将泛型类型放入函数中之前,TypeScritp能够以某种方式将T解析为具有属性的对象,并且仅枚举名称就足够了。
This happens because you are not passing in a value of the enum to the function, which would have the type EventState
but rather the object containing the enums which has type typof EventState
. 发生这种情况是因为您没有将枚举的值传递给该函数,该枚举的类型EventState
,而是将包含枚举的对象的类型为typof EventState
。 So removing the type parameter from the method, but passing in typeof EventState
to the class should work fine: 因此,从方法中删除类型参数,但将typeof EventState
传递给类应该可以正常工作:
let statusEnumMapper = new RestEnumMpapper<typeof EventState>();
Also might I suggest if the class is tied to a single enum as seems to be the case, you might as well pass that to the constructor, and add an index signature to T
to allow indexing without casting: 我也可能建议,如果似乎是将类绑定到单个枚举,则最好将其传递给构造函数,并向T
添加一个索引签名以允许在不进行强制转换的情况下进行索引:
export class RestEnumMpapper<T extends { [name: string]: any }> {
constructor(public enumObject: T) { }
getEnumAsString(key: string | number): string {
if (typeof key === 'string') {
return this.enumObject[this.enumObject[key]];
} else if (typeof key === 'number') {
return this.enumObject[key];
} else {
throw new Error(`Unable to parse enum from ${typeof (key)}`);
}
}
/* ... Rest of enum converting class code ... */
}
enum EventState {
Test = "Test",
One = "One",
}
let statusEnumMapper = new RestEnumMpapper(EventState);
let statusStr = "One";
let status = statusEnumMapper.getEnumAsString(statusStr) as EventState;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.