我想要一个静态数组类属性,其元素是子类的属性名称。例如:
class Base {
static props: (keyof this)[] = [];
}
class Sub extends Base {
static props = ['foo'];
foo = 123;
}
我需要它才能使用 Sub.props
的元素作为键:
const sub = new Sub();
for (const prop of Sub.props) {
console.log(sub[prop]);
}
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Sub'.
No index signature with a parameter of type 'string' was found on type 'Sub'.
有没有办法让这个工作? keyof this
不适用于静态属性。