示例工程 08-unions-intersections

08-unions-intersections/04-state-machine/README.md

markdown · 0.9 KB

运行:08 · state-machine

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

# 04 · state-machine
 
**演示**:用可辨识联合表达状态与事件,`reduce` 拒绝非法转移,`never` 保证事件分支完整。
**运行**:在 `tutorials/typescript` 下执行 `npm run build`,再执行 `npm run serve`,打开
`http://localhost:8080/examples/08-unions-intersections/04-state-machine/`;开 Console。
**预期控制台输出**:
```text
loading
success
idle
loading
error
```
**类型检查**:`node node_modules/typescript/bin/tsc -p examples/08-unions-intersections/04-state-machine/tsconfig.types.json`
**预期类型错误**(`demo/not-exhaustive.ts`,由 `tsconfig.fail.json` 触发):
```text
demo/not-exhaustive.ts(19,26): error TS2345: Argument of type '{ type: "cancel"; }' is not assignable to parameter of type 'never'.
```
**注意**:状态对象是不可变的,`reduce` 返回新对象;原地修改会破坏可辨识联合的假设。