【发布时间】:2023-02-03 20:41:31
【问题描述】:
我正在尝试推断泛型类属性的类型。
例子:
abstract class A<T> {
attr: T;
}
class B extends A<number> {
attr = 1;
}
type Custom = {
value: string;
};
class C extends A<Custom> {
value = "1";
}
const a: A<any> = new B();
const b: A<any> = new C();
const instances: A<any>[] = [a, b];
instances.forEach((instance) => {
// Here I need to set attr with the right type
const attr = instance.attr;
});
我怎样才能做到这一点 ? 问题可能出在指定 a 和 b 的类型时使用 any。
【问题讨论】:
标签: typescript