I have this class:
class A {
prop: string
}
and I have this class:
class B extends B {
prop?: string
}
I am getting this typeScript error:
Property 'prop' in type 'B' is not assignable to the same property in base type 'A'.
Type 'number | undefined' is not assignable to type 'number'.
Type 'undefined' is not assignable to type 'number'.ts(2416)
I want to make all of the child class properties that I am inheriting from the parent optional.
So I thought of this solution, create a function that will help TypeScript to understand that.
function OptionalizeParent<T>(cls: T): Partial<T>{
return cls
}
Then I can use it in the child class like this:
class B extends OptionalizeParent(A) {
prop?: string
}
But unfortunately, it’s not working, it’s giving me this type of error:
Type 'Partial<typeof A>' is not a constructor function type.
I know this breaks the Liskov substitution principle But I do want to break it because I don’t care about it.