对于TypeScript 1.7中的Polymorphic,我在这里发现,我们可以在类中定义一个返回类型为的方法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
}
Run Code Online (Sandbox Code Playgroud)
但是,我所追求的是拥有一个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 …Run Code Online (Sandbox Code Playgroud)