I have two components, one is parent and the other is child. Child extends from Parent. Parent has a method open(). Child overloads open() by rewriting and adding a parameter. It results in an error: open() is a property, and the property types do not match across classes.
open() => void
is not equal to
open(message: string) => void
Parent :
export class ParentClass {
constructor() { super(); }
open(){
return "Hello World!";
}
}
Child:
export class ChildClass extends ParentClass {
constructor() { super(); }
open(message: string){
return message;
}
}
The reason why this code doesn't work is simple:
let parent: ParentClass = new ParentClass();
parent.open();
parent = new ChildClass();
// what happens now?
parent.open();
After the third line of code, parent is still a type of ParentClass, so open call should be valid. On the other hand, it contains a ChildClass, so we aren't providing neccessary parameter for this method: message: string. It's a paradox.
If you want this code to be valid, both methods should share the same parameters.
Two tips for you:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With