示例工程 08-unions-intersections

08-unions-intersections/03-intersection/README.md

markdown · 0.9 KB

运行:08 · intersection

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

# 03 · intersection
 
**演示**:`A & B` 要求同时满足两侧;对象交叉合并属性,原始类型交叉得到 `never`。
**运行**:在 `tutorials/typescript` 下执行 `npm run build`,再执行 `npm run serve`,打开
`http://localhost:8080/examples/08-unions-intersections/03-intersection/`;开 Console。
**预期控制台输出**:
```text
Ada:36
admin:3
```
**类型检查**:`node node_modules/typescript/bin/tsc -p examples/08-unions-intersections/03-intersection/tsconfig.types.json`
**预期类型错误**(`demo/missing-side.ts`,由 `tsconfig.fail.json` 触发):
```text
demo/missing-side.ts(7,7): error TS2322: Type '{ name: string; }' is not assignable to type 'Person'.
  Property 'age' is missing in type '{ name: string; }' but required in type 'Aged'.
```
**注意**:交叉不会「取交集」,而是「同时满足」;对象属性取并集。