示例工程 17-pre-module-globals
17-pre-module-globals/04-script-vs-module/README.md
点击「运行示例」后,左侧显示源码、右侧显示运行结果;console 输出会实时回传。
# 04 · script-vs-module
**演示**:classic 脚本的 `var` 泄漏到 `globalThis`;模块的顶层绑定只存在于模块作用域。另有 `legacy/legacy.ts` 演示 `module: "preserve"` + `moduleDetection: "legacy"` 产出脚本。
**运行**:在 `tutorials/typescript` 下执行 `npm run build`,再执行 `npm run serve`,打开
`http://localhost:8080/examples/17-pre-module-globals/04-script-vs-module/`;开 Console。
**预期控制台输出**:
```text
classicLeak:global
moduleOnly:not-global
```
**类型检查**:`node node_modules/typescript/bin/tsc -p examples/17-pre-module-globals/04-script-vs-module/tsconfig.types.json`
**预期类型错误**(`module: "none"` 已被移除,`tsconfig.fail-module-none.json` 触发):
```text
tsconfig.fail-module-none.json(4,15): error TS6046: Argument for '--module' option must be: 'commonjs', 'es6', 'es2015', 'es2020', 'es2022', 'esnext', 'node16', 'node18', 'node20', 'nodenext', 'preserve'.
```
**注意**:脚本产物带 `"use strict"`,但这不等于模块;模块的判定看 `import`/`export` 与 `moduleDetection`。