I'm trying to pick up some TypeScript knowledge for my work, and I just realized that the implementation of method overriding in a derived class is different from other static typed languages such as Java.
For example, I realized I can do this in TS:
class Base {
constructor(greet: string){
console.log('base', greet);
}
getExample(arg1: unknown): unknown{
console.log('base getExample', arg1);
return ''
}
}
class Derived extends Base{
constructor(){
super('Hello');
}
getExample(arg1: string): string{
console.log('derived getExample', arg1);
return 'something'
}
getExample2(arg1: string): string{
console.log('derived getExample2', arg1);
return '2'
}
}
const doj: Base = new Derived();
doj.getExample(123); // derived getExample 123
doj.getExample('123'); // derived getExample “123”
(doj as Derived).getExample2('123');
I'm wondering why is the getExample in the Derived class overridden not overloaded(the argument type and return type are different from the getExample in the Base class)?
I can't find what the rules of method overriding are in the TS handbook, hope I can get help here. Many thanks.