概觀
目錄中存在 tsconfig.json
檔案表示該目錄是 TypeScript 專案的根目錄。 tsconfig.json
檔案會指定根檔案和編譯專案所需的編譯器選項。
JavaScript 專案可以使用 jsconfig.json
檔案,其作用幾乎相同,但預設會啟用一些與 JavaScript 相關的編譯旗標。
專案會使用下列其中一種方式編譯
使用 tsconfig.json
或 jsconfig.json
- 不帶任何輸入檔案呼叫 tsc,編譯器會從目前的目錄開始,沿著父目錄鏈向上搜尋
tsconfig.json
檔案。 - 不帶任何輸入檔案呼叫 tsc,並使用
--project
(或僅-p
)命令列選項,指定包含tsconfig.json
檔案的目錄路徑,或指定包含組態的有效.json
檔案路徑。
當在命令列上指定輸入檔案時,將會忽略 tsconfig.json
檔案。
範例
範例 tsconfig.json
檔案
-
使用
files
屬性{" ": {" ": "commonjs"," ": true," ": true," ": true," ": true}," ": ["core.ts","sys.ts","types.ts","scanner.ts","parser.ts","utilities.ts","binder.ts","checker.ts","emitter.ts","program.ts","commandLineParser.ts","tsc.ts","diagnosticInformationMap.generated.ts"]} -
{" ": {" ": "system"," ": true," ": true," ": true," ": "../../built/local/tsc.js"," ": true}," ": ["src/**/*"]," ": ["**/*.spec.ts"]}
TSConfig 基礎
根據您打算執行程式碼的 JavaScript 執行環境,您可以在 github.com/tsconfig/bases 使用基礎組態。這些是 tsconfig.json
檔案,您的專案會從中延伸,透過處理執行時間支援來簡化您的 tsconfig.json
。
例如,如果您正在編寫使用 Node.js 版本 12 及以上版本的專案,則可以使用 npm 模組 @tsconfig/node12
{" ": "@tsconfig/node12/tsconfig.json"," ": {" ": true}," ": ["src/**/*"]," ": ["**/*.spec.ts"]}
這讓您的 tsconfig.json
專注於專案的獨特選擇,而不是所有執行時期機制。已經有一些 tsconfig 基礎,我們希望社群可以為不同的環境新增更多基礎。
詳細資訊
"compilerOptions"
屬性可以省略,這種情況下會使用編譯器的預設值。請參閱我們支援的 編譯器選項的完整清單。
TSConfig 參考
若要深入了解 TSConfig 參考 中的數百個組態選項。
架構
tsconfig.json
架構可以在 JSON Schema Store 中找到。