我有一个通过反射得到的类型的字符串:
const paramtypes = Reflect.getMetadata('design:paramtypes', target, key);
const myParamType = paramtypes[0].name; // this returned 'Foo' as a string
现在,我想将字符串中的类型名称转换为实际的 Typescript 类型Foo。我不可能这样做typeof myParamType,因为这在逻辑上会给我字符串。
我想要实现的是这样的:
const paramtypes = Reflect.getMetadata('design:paramtypes', target, key);
const myParamType = paramtypes[0].name; // this returned 'Foo' as a string
// My objective is to convert the 'Foo' to Foo type that I can use it as a regular type
function myFunc(): typeof myParamType { // I want the return type to be Foo but it will end up as string
...
}
如何从字符串类型的名称中获取类型?