There is a github issue on Polymorphic this in static methods and also a related question here. These are discussing how to solve/work around the problem in case of static class methods. However I could not find any reference to solve this problem in case of static class members. We have a model class and this class has a few static members containing maps where the keys are the model fields. We are using property decorators in the inherited classes to tag model fields (the inherited classes containing only field definitions). The decorator is also adding the fields to the static maps defined in the base class. See the code below and in playgound
function field(): any {
return function (target: Base, propertyKey: ModelFields<Base>, _descriptor: PropertyDescriptor) {
if (!target.constructor.fields) {
target.constructor.fields = {};
}
target.constructor.fields[String(propertyKey)] = String(`s_${propertyKey}`);
};
}
class Base {
['constructor']: typeof Base;
static fields: Record<string, string>;
// Actually the above is supposed to be Record<keyof ModelFields<this>, string> but 'this' is not allowed here
}
class A extends Base {
@field()
public propA: string = 'A';
}
class B extends Base {
@field()
public propB: string = 'B';
}
type ModelFields<T extends Base> = Required<Omit<T, keyof Base>>
console.log(B.fields) // B.fields type is not correct here
currently the static fields is defined as Record<string, string> but it doesn't tell what keys exists on it although we know that the valid keys are keyof ModelFields<this>. But obviously this is not allowed there.
Is there any solution to get the typing of fields right?