【发布时间】:2016-12-19 10:50:17
【问题描述】:
TypeScript 编译器在以下代码示例中给我一个错误,尽管 https://www.typescriptlang.org/play/ 上生成的 JavaScript 按预期工作
错误是:error TS2339: Property 'tableName' does not exist on type 'Function'.
class ActiveRecord {
static tableName(): string { // override this
return "active_record";
}
static findOne(): any {
return 'Finding a record on table: ' + this.tableName();
}
save(): void {
console.log('Saving record to table: ' + this.constructor.tableName());
}
}
class MyModel extends ActiveRecord {
static tableName(): string {
return "my_model";
}
}
let x = new MyModel();
x.save(); // "Saving record on table: my_model"
console.log(MyModel.findOne()); // "Finding a record on table: my_model"
有什么办法可以解决这个错误吗?
【问题讨论】:
-
使用 ActiveRecord.tableName() 不会调用子类的重写方法,这会破坏@Joe 的预期行为
标签: javascript typescript