I'm attempting to access a static property defined in a child class from a non-static method in the parent class, but I can't seem to find a way to make it work. Is this scenario possible at all?
class Parent {
static staticList: {
a: number
} = {
a: 1
};
public test<TParent extends Parent>(this: TParent, key: keyof TParent['constructor']['staticList']) { /** struggling to reference the constructor class of the 'this' parameter here */
console.log(this.constructor.staticList[key]); /** although this logs the correct information, TypeScript is giving me an error - why? */
}
}
class Child extends Parent {
static staticList: {
a: number
b: number
} = {
a: 2,
b: 2
}
}
const childInstance = new Child();
childInstance.test('a'); /** this correctly logs out 2 and should suggest "a" or "b" for type hinting, not "never" */
Check out my problem on TS Playground