代码点读机 / TypeScript / implements

implements TypeScript · 关键字

类签了一份协议,保证自己提供 interface 要求的所有服务,缺一项就违约。

怎么用

class 类名 implements 接口名 {}

小例子

interface 能飞 {
  飞(): void;
}
class 鸟 implements 能飞 {
  飞() {}
}

类型检查结果

鸟 正确实现 能飞

提醒:接口只要声明,类必须实现

官方文档:https://www.typescriptlang.org/docs/handbook/2/classes.html#implements-clauses

在代码里遇到不认识的词?打开代码点读机,点一下就懂 → ← TypeScript全部词条