tsconfig.json 被 tsc 和编辑器里的 TypeScript 语言服务读取。它规定哪些文件属于这个工程,以及按什么规则做类型检查。这里 noEmit 为真,所以它不生成 .js。Node 执行 .ts 时只删除类型,不读这份文件;模块按 CommonJS 还是 ES Module 运行,由 package.json 的 "type" 决定。
{
"compilerOptions": {
"target": "ES2023",
"module": "preserve",
"moduleResolution": "bundler",
"lib": ["ES2023"],
"types": ["node"],
"strict": true,
"noEmit": true,
"allowImportingTsExtensions": true,
"verbatimModuleSyntax": true,
"erasableSyntaxOnly": true,
"skipLibCheck": true,
"noUncheckedIndexedAccess": true,
"forceConsistentCasingInFileNames": true
},
"include": ["src", "test", "bin"]
}| 选项 | 作用 |
|---|---|
target: "ES2023" | 按 ES2023 判断哪些语法合法 |
module: "preserve" | 模块写法保持文件里的原样,不把 import 翻译成 require |
moduleResolution: "bundler" | 解析导入路径时允许相对路径不写扩展名 |
lib: ["ES2023"] | 内置对象用 ES2023 的类型 |
types: ["node"] | 全局自动引入 @types/node |
strict: true | 打开严格检查:隐式 any、null / undefined、未初始化属性等 |
noEmit: true | 只检查,不生成 .js 或 .d.ts |
allowImportingTsExtensions: true | 允许 import ... from './file.ts' |
verbatimModuleSyntax: true | 导入导出必须和 Node 删掉类型后留下的一致 |
erasableSyntaxOnly: true | 只允许删掉类型后仍是合法 JavaScript 的语法 |
skipLibCheck: true | 不检查依赖里的 .d.ts 内容是否正确 |
noUncheckedIndexedAccess: true | 下标访问的类型包含 undefined |
forceConsistentCasingInFileNames: true | 导入路径的大小写必须和真实文件名一致 |
include | 参与检查的目录 |