示例工程 16-js-to-ts-migration
16-js-to-ts-migration/01-jsdoc-types/README.md
点击「运行示例」后,左侧显示源码、右侧显示运行结果;console 输出会实时回传。
# 01 · jsdoc-types
**演示**:用 `checkJs` + JSDoc 给现有的 `.js` 加类型检查,不改文件扩展名。
**运行**:在 `tutorials/typescript` 下执行 `npm run build`,再执行 `npm run serve`,打开
`http://localhost:8080/examples/16-js-to-ts-migration/01-jsdoc-types/`;页面无输出,测试会导入 `dist-js/legacy.js`。
**类型检查**:`node node_modules/typescript/bin/tsc -p examples/16-js-to-ts-migration/01-jsdoc-types/tsconfig.jsdoc.json`
**预期类型错误**(`js/broken.js`,由 `tsconfig.fail.json` 触发):
```text
js/broken.js(8,3): error TS2322: Type 'string' is not assignable to type 'number'.
```
**预期类型错误之二**(`js/closure-syntax.js`,由 `tsconfig.fail-closure.json` 触发;TS 7 不再支持 Closure 风格函数签名):
```text
js/closure-syntax.js(4,20): error TS1005: '}' expected.
```
**注意**:`checkJs` 对未加 `// @ts-check` 的文件也生效(因为配置里开了 `checkJs: true`);单文件开关见第 02 个示例。