My goal is to inherit a class and modify its dynamic type to collapse into a specific type (from T
to Person
).
In order to demonstrate this, I have created some dummy classes:
class Person {
constructor(public name: string){}
}
class Base<T> {
reference?: T
name: string
constructor(name: string) {
// Assign properties from T to this
this.name = name
}
static fromObject<T extends { name: string }>(object: T){
const base = new Base(object.name)
base.reference = object
return base
}
}
class Master<T> extends Base<T> {
static fromBase<T>(base: Base<T>){
const master = new Master(base.name)
master.reference = base.reference
return master
}
static fromObject<T extends { name: string }>(object: T){
return Master.fromBase(Base.fromObject(object))
}
}
class PersonMaster extends Master<Person> {
constructor(person: Person){
super(person.name)
this.reference = person
}
static fromBase(base: Base<Person>){
return new PersonMaster(base)
}
}
The issue arises when the compiler returns an error message stating that Class static side typeof PersonMaster
incorrectly extends base class static side typeof Master
. The properties of fromBase
are not compatible. The Type
(base: Base<Person>) => PersonMaster
cannot be assigned to <T>(base: Base<T>) => Master<>
. This is due to incompatibility between types of parameters base
and base
, as well as the fact that Base<T>
is not assignable to Base<Person>
because T
does not match with type Person
.