I'd like to write something like this in Typescript:
export function stringToEnum<T>(enumObj: T, str: string): keyof T {
return enumObj[str];
}
and use it as follows:
enum MyEnum {
Foo
}
stringToEnum<MyEnum>(MyEnum, 'Foo');
where it would return
MyEnum.Foo
The function above works as expected... but the typings are throwing errors. For the parameter MyEnum
in stringToEnum<MyEnum>(MyEnum, 'Foo');
, Typescript complains tha:
Argument of type 'typeof MyEnum' is not assignable to parameter of type 'MyEnum'
which makes sense... unfortunately. Any ideas on how I can get around this?