【发布时间】:2020-07-03 10:14:52
【问题描述】:
我在将元素“子”与泛型类型 T 进行比较并返回仅包含这些类型的子元素的数组时遇到了麻烦。
逻辑很简单,我有一个具有不同元素的子数组,它们都继承自“Test”类型。我只想要一部分“特殊”孩子。
我现在似乎能够找到一个解决方案,以任何方式将泛型类型 T 与子类型进行比较。
产生的错误是
TS2693:'T' 仅指一种类型,但在此处用作值
我用instanceof、typeof、Object.getPrototypeOf 尝试过,没有任何效果。
public getChildrenByType<T extends Test>(): T[] {
const output:T[] = [];
for(const child of this.children) {
if(child instanceof T) {
output.push(child);
}
}
}
return output;
}
【问题讨论】:
标签: typescript typescript-generics typescript3.8