【发布时间】:2021-02-18 00:01:51
【问题描述】:
我最近偶然发现了以下场景:
class A {
public m1(x: string | string[]): string | string[] {
return this.m2(x);
}
protected m2(x: string | string[]): string | string[] {
return x;
}
}
class B extends A {
protected m2(x: string[]): string { // no compiler warning
return x.join(',');
}
}
const b = new B();
console.log(b.m1(['a', 'b', 'c'])); // ok
console.log(b.m1('d')); // runtime error
这是 TypeScript 打字系统中的错误还是故意的?如果是后者,如何更改类型以便编译器识别问题?
【问题讨论】:
标签: typescript inheritance methods overriding