【发布时间】:2015-12-04 21:47:34
【问题描述】:
使用 TypeScript 1.7 中的Polymorphic this,正如我发现的here,我们可以在返回类型为this 的类中定义一个方法,并且自动地,任何扩展该类并继承方法的类将将它们的返回类型设置为各自的this 类型。像这样:
class Model {
save():this { // return type: Model
// save the current instance and return it
}
}
class SomeModel extends Model {
// inherits the save() method - return type: SomeModel
}
但是,我所追求的是继承static 方法,其返回类型引用类本身。最好在代码中描述:
class Model {
static getAll():Model[] {
// return all recorded instances of Model as an array
}
save():this {
// save the current instance and return it
}
}
class SomeModel extends Model {
// inherits the save() method - return type: SomeModel
// also inherits getAll() - return type: Model (how can we make that SomeModel?)
}
也许我必须想出一种不同的方法来实现它,因为 TypeScript 1.7 中的多态 this 不支持 static 方法设计。
编辑:我想我们会看到这个 Github 问题如何结束:https://github.com/Microsoft/TypeScript/issues/5863
【问题讨论】:
标签: generics inheritance static typescript self-reference