接口字段应具有实现该接口的类的类型-typescript

时间:2019-06-29 18:49:24

标签: typescript types interface typing

我想知道这是否可以在Typescript中实现。

interface Pine {

    child : <The class that implements this interface>
}

class Vine implements Pine {

    child : Vine // this is enforced to be Vine, rather than any object that implements Pine.
}

我从this question知道您无法用Java做到这一点,但是TS呢?我查了一下tslang doc却没有结果。

1 个答案:

答案 0 :(得分:0)

您可以使用泛型。 例如:

interface Base<T> {
    child: T;
}

class Derived implements Base<string> {
    child: string;
} 

class Derived2 implements Base<number> {
    child: number;
}