示例工程 28-events-async-browser-types

28-events-async-browser-types/02-custom-event/README.md

markdown · 0.6 KB

运行:28 · custom-event

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

# 02 · custom-event
 
**演示**:`CustomEvent<T>` 把 `detail` 的类型写进事件;默认 `CustomEvent` 的 `detail` 是 `any`。
**运行**:在 `tutorials/typescript` 下执行 `npm run build`,再执行 `npm run serve`,打开
`http://localhost:8080/examples/28-events-async-browser-types/02-custom-event/`;开 Console。
**预期控制台输出**:
```text
toast:saved
```
**类型检查**:`node node_modules/typescript/bin/tsc -p examples/28-events-async-browser-types/02-custom-event/tsconfig.types.json`
**注意**:`addEventListener('toast', ...)` 不会自动推断成 `CustomEvent<ToastDetail>`;需要自己收窄/断言。