Mer*_*ott 15 generics inheritance static self-reference typescript
对于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 {
// inherits the save() method - return type: SomeModel
// also inherits getAll() - return type: Model (how can we make that SomeModel?)
}
Run Code Online (Sandbox Code Playgroud)
也许我必须考虑一种不同的方式来实现它,因为thisTypeScript 1.7中的Polymorphic 不支持设计static方法.
编辑:我想我们会看到这个Github问题是如何结束的:https://github.com/Microsoft/TypeScript/issues/5863
Bri*_*unt 21
基于最简单的回答到GitHub的问题,你可以使用InstanceType<>这样的:
class Foo {
static create<T extends typeof Foo>(this: T): InstanceType<T> {
return new this() as InstanceType<T>
}
static getAll<T extends typeof Foo>(this: T): Array<InstanceType<T>> {
return []
}
}
class Bar extends Foo { }
const a = Bar.getAll() // typeof a is Bar[]
const b = Bar.create() // typeof b is Bar.
Run Code Online (Sandbox Code Playgroud)
我create从链接的 GitHub 示例中插入函数只是为了说明。
mrm*_*mrm 20
这在TypeScript 2.0+中是可行的.通过使用内联{ new(): T }类型捕获this,您将获得您想要的:
class BaseModel {
static getAll<T>(this: { new(): T }): T[] {
return [] // dummy impl
}
save(): this {
return this // dummy impl
}
}
class SubModel extends BaseModel {
}
const sub = new SubModel()
const savedSub: SubModel = sub.save()
const savedSubs: SubModel[] = SubModel.getAll()
Run Code Online (Sandbox Code Playgroud)
请注意,getAll仍然希望此类型没有参数.
有关详细信息,请参阅https://www.typescriptlang.org/docs/handbook/generics.html#using-class-types-in-generics和/sf/answers/3168360191/
| 归档时间: |
|
| 查看次数: |
10202 次 |
| 最近记录: |