TypeScript:当前类的类型作为类型变量

bil*_*.cn 6 generics typescript

我想在TypeScript中声明一个函数,该函数返回当前实例类型的类型,例如在返回的流畅API中this.

是否有"此类型"的类型变量?(我宁愿不使用丑陋的Java方式X<T extends X<T>>.)

Rya*_*ugh 8

在TypeScript中还没有办法做到这一点.有关投票/讨论,请参阅问题https://github.com/Microsoft/TypeScript/issues/285.


cra*_*bus 6

现在可以实现流畅的接口:

class FluentInterface {
    aMethod(): this {
        return this;
    }
}
Run Code Online (Sandbox Code Playgroud)