【发布时间】:2021-09-03 07:54:23
【问题描述】:
我使用以下代码收到此错误: 'realType' 指的是一个值,但在这里被用作一个类型。你是说'typeof realType'吗?(
class Quux{};
class Foo extends Quux{};
class Bar extends Quux{};
const quuxBundle: Record<string, typeof Quux> = {
"Foo": Foo,
"Bar": Bar
};
function lookupQuuxType(typeName: string): typeof Quux | undefined {
return quuxBundle[typeName];
}
function fetchQuux<T extends Quux() {
const result = {} as T;
console.log('result', typeof result);
return result;
}
const realType = lookupQuuxType('Foo');
fetchQuux<realType>();
【问题讨论】:
-
首先,您发送的代码甚至不是有效的语法(
<T extends Quux() {应该是什么?),而且 Typescript 也是一个纯粹的编译时类型系统,它没有对运行时的影响。
标签: typescript generics