示例工程 07-classes-interfaces

07-classes-interfaces/01-implements/README.md

markdown · 0.9 KB

运行:07 · implements

点击「运行示例」后,左侧显示源码、右侧显示运行结果;console 输出会实时回传。

# 01 · implements
 
**演示**:`implements Greeter` 只做形状检查;类不会因此获得任何运行时成员或原型关系。
**运行**:在 `tutorials/typescript` 下执行 `npm run build`,再执行 `npm run serve`,打开
`http://localhost:8080/examples/07-classes-interfaces/01-implements/`;开 Console。
**预期控制台输出**:
```text
Hello
你好
```
**类型检查**:`node node_modules/typescript/bin/tsc -p examples/07-classes-interfaces/01-implements/tsconfig.types.json`
**预期类型错误**(`demo/missing-member.ts`,由 `tsconfig.fail.json` 触发):
```text
demo/missing-member.ts(7,7): error TS2420: Class 'Broken' incorrectly implements interface 'Greeter'.
  Property 'greet' is missing in type 'Broken' but required in type 'Greeter'.
```
**注意**:`implements` 不改变原型链,`instance instanceof Greeter` 不成立(接口没有运行时值)。