示例工程 28-events-async-browser-types
28-events-async-browser-types/03-fetch-json/README.md
点击「运行示例」后,左侧显示源码、右侧显示运行结果;console 输出会实时回传。
# 03 · fetch-json
**演示**:`fetch` 只在网络错误时 reject;4xx 要自己查 `response.ok`;`response.json()` 是 `any`,必须转 `unknown` 再校验。
**运行**:在 `tutorials/typescript` 下执行 `npm run build`,再执行 `npm run serve`,打开
`http://localhost:8080/examples/28-events-async-browser-types/03-fetch-json/`;开 Console。
**预期控制台输出**:
```text
Ada
```
**类型检查**:`node node_modules/typescript/bin/tsc -p examples/28-events-async-browser-types/03-fetch-json/tsconfig.types.json`
**预期类型错误**(`demo/unknown-json.ts`,由 `tsconfig.fail.json` 触发):
```text
demo/unknown-json.ts(5,21): error TS18046: 'data' is of type 'unknown'.
```
**注意**:真实项目里 4xx 的 body 可能是 JSON 错误信息,也要先读再判断。