I created a class with several parameters that are initialized in the constructor, and then I have another class that extends this one. Here is an example with simplified parameters:
class MyClass {
name?: string;
day?: Date;
constructor({
name,
day,
}: { name?: string, day?: Date } = {}) {
this.name = name;
this.day = day;
}
}
class MySpecialClass extends MyClass {
isSpecial?: boolean;
constructor({
isSpecial,
...options
}: { isSpecial?: boolean } = {}) {
super(options)
this.isSpecial = isSpecial
}
}
The issue here is that I haven't specified the type of options
, making it hard to utilize coding suggestions and prone to errors.
const specialInstance = new MySpecialClass({ isSpecial: true, name: "sample" })
// 'name' does not exist in type '{ isSpecial?: boolean | undefined; }'
What is the correct approach to solve this problem? Rewriting all parameters manually is one solution but can lead to mistakes if some are missed.
If your solution also addresses the original MyClass
by automatically extracting parameters instead of rewriting them, that would be highly beneficial.