示例工程 05-function-types-overloads

05-function-types-overloads/04-generic-identity/README.md

markdown · 0.6 KB

运行:05 · generic-identity

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

# 04 · generic-identity
 
**演示**:`identity<T>` 保留字面量类型;`NoInfer<T>` 阻止 `fallback` 把 `T` 拓宽。
**运行**:在 `tutorials/typescript` 下执行 `npm run build`,再执行 `npm run serve`,打开
`http://localhost:8080/examples/05-function-types-overloads/04-generic-identity/`;开 Console。
**预期控制台输出**:
```text
ts
1
2,4,6
0:a|1:b
```
**类型检查**:`node node_modules/typescript/bin/tsc -p examples/05-function-types-overloads/04-generic-identity/tsconfig.types.json`
**注意**:`firstOr([1, 2], 'x')` 在负例里必须报错;去掉 `NoInfer` 会退化成 `number | string`。