【发布时间】:2021-10-26 01:42:39
【问题描述】:
我正在尝试重载/覆盖 TypeScript 中的超类方法,其中从属方法采用一个参数,而上级方法采用零。
我的 TypeScript 代码类似于以下内容:
// index.ts
class Super {
create(): void {}
}
class Sub extends Super {
create(arg: any): void {}
}
let sub = new Sub();
sub.create(1);
此代码产生以下错误:
Property 'create' in type 'Sub' is not assignable to the same property in base type 'Super'.
Type '(arg: any) => void' is not assignable to type '() => void'. ts(2416)
为什么这不起作用?是因为转译后的 JavaScript 输出根本无法区分这两个类的继承关系吗?我将如何完成这样的事情?
【问题讨论】:
标签: javascript typescript oop abstract-class