Trying to understand typescript generics + static class functions a bit more thoroughly, and I am running into to some type problems:
class Base {
value : string = 'default'
static identity<M extends typeof Base>() : M {
return this // Type Error: Type 'typeof Base' is not assignable to type 'M'
}
}
class Extended extends Base {
otherValue = 'extended'
constructor() {
super()
}
}
let baseClass = Base.identity()
let extendedClass = Extended.identity()
expect(new baseClass().value).to.eq('default')
// Type Error: Property 'otherValue' does not exist on type 'Base'
// How do I make Extended.identity() be `typeof Extended` without
// an explicit cast?
expect(new extendedClass().otherValue).to.eq('extended')
If I ignore type errors and run the output code, everything runs as expected and expectations are met. I think this is probably due to some understanding gaps with static functions, but any help in understanding the problem would be much appreciated.