简体   繁体   English

如何定义“实现”接口的类型?

[英]How can I define a type that “implements” an interface?

Classes can implement interfaces: 类可以实现接口:

interface ClockInterface {
    currentTime: Date;
}

class Clock implements ClockInterface {
    currentTime: Date;
    constructor(h: number, m: number) { }
}

How can I define a type that "implements" an interface? 如何定义“实现”接口的类型? I don't understand why the following does not work, as it looks completely reasonable to me: 我不明白以下原因为何不起作用,因为在我看来,这完全合理:

type DigitalClock implements ClockInterface = {
    currentTime: Date;
    somethingDigital: any;
}

Just use 只需使用

type DigitalClock = ClockInterface & {
    somethingDigital: any;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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