以下是代表我的问题的一段代码:
class Parent<T = unknown> {
constructor(private prop: T) {}
getProp(): T {
return this.prop;
}
}
class Child extends Parent {
constructor() {
super({
baba: 10,
bobo: 20
});
}
}
const instance = new Child();
const prop = instance.getProp();
// |
// '-> prop is of type `unknown`
我想让TypeScrip理解父类的类型,只是使用对象值,而不是显式地给它类型.
在这种情况下,我们应该有Parent<{baba: number, bobo: number}>
这有可能吗?