I recently came across a puzzling situation:
class A {
public method1(x: string | string[]): string | string[] {
return this.method2(x);
}
protected method2(x: string | string[]): string | string[] {
return x;
}
}
class B extends A {
protected method2(x: string[]): string { // no compiler warning
return x.join(',');
}
}
const b = new B();
console.log(b.method1(['a', 'b', 'c'])); // ok
console.log(b.method1('d')); // runtime error
Is this an issue with TypeScript's typing system or intentional behavior? If the latter, what adjustments can be made to the typing to catch these errors during compilation?