I have a class with few parameters, that are initialized in the class constructor, and then I have a second class that extends
the former. For instance (the number of parameters is actually much bigger):
class MyClass {
name?: string;
day?: Date;
constructor({
name,
day,
}: { name?: string, day?: Date } = {}) {
this.name = name;
this.day = day;
}
}
class MyAwesomeClass extends MyClass {
isAwesome?: boolean;
constructor({
isAwesome,
...props
}: { isAwesome?: boolean } = {}) {
super(props)
this.isAwesome = isAwesome
}
}
The problem with the above is that I am not specifying the type of props
and, altough I am not getting a compiling error, I am not able to exploit coding suggestions and it is much more error prone.
const awesome = new MyAwesomeClass({ isAwesome: true, name: "test" })
// 'name' does not exist in type '{ isAwesome?: boolean | undefined; }'
What is the proper way to handle it? I may re-write all the parameters, but since I have plenty of them it is again very error prone, as I may miss a couple without noticing.
BTW, if your solution also helps with original MyClass
, i.e. automatically extracting parameters instead of needing to rewrite them all, that would be great.
2
Answers
One way is just to extract the props as a type, and then you can union the props on the inherited.
eg.
Working example
TS Playground
If you wish copy the type from the Constructor, you can do -> Is there a way to inherit parameters from a parent class's constructor? But personally I think that looks more complicated than just extracting the Types..
And if using
ConstructorParameters
->TS Playground
I’ll leave for others to decide what is the easiest to implement / understand..
Here is a helper type to let you do that:
https://tsplay.dev/WvyDMm
It is possible to extend this for multiple arguments.
If all of the class properties are its constructor parameters this may be simplified