例如:
class Super {
getProp(key: ???) {
return this[key];
}
}
class Sub extends Super {
foo = 123;
}
const sub = new Sub();
console.log(sub.getProp('foo'));
的类型是否可能key
是“子类的任何属性”?
只是 keyof this
class Super {
getProp(key: keyof this) {
return this[key];
}
}
class Sub extends Super {
foo = 123;
}
const sub = new Sub();
console.log(sub.getProp('foo'));
console.log(sub.getProp('something')); // Error
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句