简体   繁体   中英

Interface field should have type of the class that implements that interface - typescript

I am wondering if this is achievable in 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.
}

I know from this question that you cannot do it in Java, but what about TS? I have looked up tslang doc without fruition.

You can use Generics. eg :

interface Base<T> {
    child: T;
}

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM