示例工程 13-declaration-files
13-declaration-files/01-ambient-declaration/README.md
点击「运行示例」后,左侧显示源码、右侧显示运行结果;console 输出会实时回传。
# 01 · ambient-declaration
**演示**:给没有类型的全局 JS 库(`lib/legacy-math.js`)手写 `types/legacy-math.d.ts`,让 TS 知道它的形状。
**运行**:在 `tutorials/typescript` 下执行 `npm run build`,再执行 `npm run serve`,打开
`http://localhost:8080/examples/13-declaration-files/01-ambient-declaration/`;开 Console。
**预期控制台输出**:
```text
3
1.0.0
```
**类型检查**:`node node_modules/typescript/bin/tsc -p examples/13-declaration-files/01-ambient-declaration/tsconfig.types.json`
**预期类型错误**(`demo/untyped-import.ts`,由 `tsconfig.fail.json` 触发):
```text
demo/untyped-import.ts(3,21): error TS7016: Could not find a declaration file for module '../lib/legacy-math.js'. '.../lib/legacy-math.js' implicitly has an 'any' type.
```
**注意**:`.d.ts` 没有顶层 `export`,所以它是全局脚本声明;有 `export` 就会变成模块,全局名不可见。