tsconfig.json 配置清單
基礎結構
{"compilerOptions": {},"include": [],"exclude": [],"extends": "./base-tsconfig.json","files": [],"references": []
}
compilerOptions 詳細配置
按類別整理,每個配置項都附帶解釋和可能的值。
1. ECMAScript 版本、模塊系統
配置項 | 說明 | 可能的值 |
---|
target | 目標 ECMAScript 版本 | ES3 , ES5 , ES6 , ES2015 , ES2016 , ES2017 , ES2018 , ES2019 , ES2020 , ES2021 , ES2022 , ESNext |
module | 目標模塊系統 | CommonJS , AMD , UMD , System , ES6 , ES2015 , ES2020 , ESNext |
lib | 需要包含的庫 | ["ES6", "DOM", "ESNext", "ES2020", "ES2019"] |
moduleResolution | 模塊解析策略 | Classic , Node , Bundler |
moduleDetection | 何時啟用模塊支持 | auto , legacy , force |
rootDirs | 指定多個根目錄 | ["src", "shared"] |
allowJs | 允許編譯 .js 文件 | true , false |
checkJs | 啟用 JS 文件類型檢查 | true , false |
resolveJsonModule | 允許導入 JSON 文件 | true , false |
esModuleInterop | 兼容 ES 模塊默認導入 | true , false |
preserveSymlinks | 不解析軟鏈接 | true , false |
2. 輸出控制
配置項 | 說明 | 可能的值 |
---|
outDir | 指定輸出目錄 | "./dist" |
outFile | 輸出為單個 .js 文件 | "./bundle.js" |
rootDir | 指定源代碼根目錄 | "./src" |
declaration | 生成 .d.ts 聲明文件 | true , false |
declarationMap | 生成 .d.ts.map 映射文件 | true , false |
declarationDir | .d.ts 輸出目錄 | "./types" |
sourceMap | 生成 .map 文件 | true , false |
inlineSourceMap | 內聯 sourceMap | true , false |
removeComments | 移除注釋 | true , false |
emitBOM | 輸出帶有 BOM 頭的文件 | true , false |
3. 嚴格模式
配置項 | 說明 | 可能的值 |
---|
strict | 開啟所有嚴格模式 | true , false |
noImplicitAny | 禁止隱式 any 類型 | true , false |
strictNullChecks | 啟用嚴格的空值檢查 | true , false |
strictFunctionTypes | 啟用函數參數嚴格檢查 | true , false |
strictBindCallApply | 啟用 bind 、call 、apply 檢查 | true , false |
strictPropertyInitialization | 要求類屬性初始化 | true , false |
useUnknownInCatchVariables | catch 語句變量默認 unknown | true , false |
4. 代碼檢查
配置項 | 說明 | 可能的值 |
---|
noUnusedLocals | 禁止未使用的局部變量 | true , false |
noUnusedParameters | 禁止未使用的函數參數 | true , false |
noImplicitReturns | 要求函數必須有返回值 | true , false |
noFallthroughCasesInSwitch | 禁止 switch 語句 fallthrough | true , false |
allowUnreachableCode | 允許無法訪問的代碼 | true , false |
5. ES 特性支持
配置項 | 說明 | 可能的值 |
---|
experimentalDecorators | 啟用實驗性的裝飾器 | true , false |
emitDecoratorMetadata | 生成裝飾器的元數據 | true , false |
downlevelIteration | 向低版本 ES 兼容迭代器 | true , false |
6. 其他
配置項 | 說明 |
---|
baseUrl | 模塊解析的基準路徑 |
paths | 配置模塊別名 |
types | 指定要包含的類型定義 |
allowSyntheticDefaultImports | 允許默認導入非 ES6 模塊 |
forceConsistentCasingInFileNames | 強制文件名大小寫一致 |
7. include
、exclude
、files
{"include": ["src/**/*"],"exclude": ["node_modules", "dist"],"files": ["index.ts", "types.d.ts"]
}
8. extends
{"extends": "./base-tsconfig.json"
}
9. references
{"references": [{ "path": "./packages/core" },{ "path": "./packages/utils" }]
}
10. 示例
{"compilerOptions": {"target": "ES2020","module": "CommonJS","strict": true,"outDir": "./dist","rootDir": "./src","sourceMap": true,"resolveJsonModule": true,"allowJs": true,"incremental": true},"include": ["src/**/*"],"exclude": ["node_modules", "dist"]
}