示例工程 27-dom-web-types

27-dom-web-types/01-query-selector/README.md

markdown · 0.6 KB

运行:27 · query-selector

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

# 01 · query-selector
 
**演示**:`querySelector` 默认返回 `Element | null`;泛型参数可以精确到 `HTMLInputElement`。
**运行**:在 `tutorials/typescript` 下执行 `npm run build`,再执行 `npm run serve`,打开
`http://localhost:8080/examples/27-dom-web-types/01-query-selector/`;开 Console。
**预期控制台输出**:
```text
Ada
```
**类型检查**:`node node_modules/typescript/bin/tsc -p examples/27-dom-web-types/01-query-selector/tsconfig.types.json`
**注意**:泛型只影响类型,运行时仍按选择器返回元素;找不到时是 `null`,必须判空。