tsc CLI 選項

使用 CLI

在本地執行 tsc 將會編譯由 tsconfig.json 定義的最近的專案,或者你可以透過傳入一組想要編譯的 TypeScript 檔案(使用 glob 模式)來進行編譯。當在命令列中指定輸入檔案時,tsconfig.json 檔案將被忽略。

sh
# Run a compile based on a backwards look through the fs for a tsconfig.json
tsc
# Emit JS for just the index.ts with the compiler defaults
tsc index.ts
# Emit JS for any .ts files in the folder src, with the default settings
tsc src/*.ts
# Emit files referenced in with the compiler settings from tsconfig.production.json
tsc --project tsconfig.production.json
# Emit d.ts files for a js file with showing compiler options which are booleans
tsc index.js --declaration --emitDeclarationOnly
# Emit a single .js file from two files via compiler options which take string arguments
tsc app.ts util.ts --target esnext --outfile index.js

編譯器選項

如果您想了解有關 tsconfig 中編譯器選項的更多資訊,請檢視 TSConfig 參考

CLI 命令

標誌 型別
--all

boolean

顯示所有編譯器選項。

--help

boolean

提供有關 CLI 的本地幫助資訊。

--ignoreConfig

boolean

忽略已找到的 tsconfig,並使用命令列選項和檔案進行構建。

--init

boolean

初始化一個 TypeScript 專案並建立一個 tsconfig.json 檔案。

--listFilesOnly

boolean

列印參與編譯的檔名,然後停止處理。

--locale

string

設定來自 TypeScript 的訊息語言。這不會影響輸出結果。

--project

string

編譯指定路徑下的專案(指向配置檔案,或包含 'tsconfig.json' 的資料夾)。

--showConfig

boolean

列印最終配置,而不執行構建。

--version

boolean

列印編譯器版本。

構建選項

標誌 型別
--build

boolean

構建一個或多個專案及其依賴項(如果它們已過期)。

--clean

boolean

刪除所有專案的輸出。

--dry

boolean

展示將會構建(或刪除,如果指定了 '--clean')的內容。

--force

boolean

構建所有專案,包括那些看起來是最新的專案。

--verbose

boolean

啟用詳細日誌記錄。

監視選項

標誌 型別
--excludeDirectories

列表

從監視程序中排除一系列目錄。

--excludeFiles

列表

從監視模式的程序中排除一系列檔案。

--fallbackPolling

fixedinterval, priorityinterval, dynamicpriority, 或 fixedchunksize

指定當系統耗盡原生檔案監視器時,監視器應採用何種方法。

--synchronousWatchDirectory

boolean

在不支援原生遞迴監視的平臺上,同步呼叫回撥並更新目錄監視器的狀態。

--watch

boolean

監視輸入檔案。

--watchDirectory

usefsevents, fixedpollinginterval, dynamicprioritypolling, 或 fixedchunksizepolling

指定在缺乏遞迴檔案監視功能的系統上如何監視目錄。

--watchFile

fixedpollinginterval, prioritypollinginterval, dynamicprioritypolling, fixedchunksizepolling, usefsevents, 或 usefseventsonparentdirectory

指定 TypeScript 監視模式的工作方式。

編譯器標誌

標誌 型別 預設
--allowArbitraryExtensions

boolean

false

啟用匯入帶有任意副檔名的檔案,前提是存在宣告檔案。

--allowImportingTsExtensions

boolean

如果開啟了 rewriteRelativeImportExtensions 則為 true;否則為 false

允許匯入語句包含 TypeScript 副檔名。

--allowJs

boolean

false,除非設定了 checkJs

允許 JavaScript 檔案成為程式的一部分。使用 checkJS 選項可從這些檔案中獲取錯誤資訊。

--allowSyntheticDefaultImports

boolean

如果啟用了 esModuleInteropmodulesystem,或 moduleResolutionbundler,則為 true;否則為 false

當模組沒有預設匯出時,允許使用 'import x from y'。

--allowUmdGlobalAccess

boolean

false

允許從模組中訪問 UMD 全域性變數。

--allowUnreachableCode

boolean

停用對不可達程式碼的錯誤報告。

--allowUnusedLabels

boolean

停用對未使用標籤的錯誤報告。

--alwaysStrict

boolean

如果開啟了 strict 則為 true;否則為 false

確保始終輸出 'use strict'。

--assumeChangesOnlyAffectDirectDependencies

boolean

false

在同時使用 incrementalwatch 模式的專案中,使重新編譯時假定檔案內的更改只會影響直接依賴於該檔案的檔案。

--baseUrl

string

指定解析非相對模組名稱的基礎目錄。

--charset

string

utf8

不再支援。在早期版本中,用於手動設定讀取檔案的文字編碼。

--checkJs

boolean

false

在經過型別檢查的 JavaScript 檔案中啟用錯誤報告。

--composite

boolean

false

啟用允許將 TypeScript 專案與專案引用(project references)一起使用的約束。

--customConditions

列表

在解析匯入時,除了解析器特定的預設值外,還需要設定的條件。

--declaration

boolean

如果開啟了 composite 則為 true;否則為 false

從專案中的 TypeScript 和 JavaScript 檔案生成 .d.ts 檔案。

--declarationDir

string

指定生成的宣告檔案的輸出目錄。

--declarationMap

boolean

false

為 d.ts 檔案建立源對映(sourcemap)。

--diagnostics

boolean

false

構建後輸出編譯器效能資訊。

--disableReferencedProjectLoad

boolean

false

減少 TypeScript 自動載入的專案數量。

--disableSizeLimit

boolean

false

取消 TypeScript 語言伺服器中 JavaScript 檔案總原始碼大小 20MB 的限制。

--disableSolutionSearching

boolean

false

在編輯時讓專案退出多專案引用檢查。

--disableSourceOfProjectReferenceRedirect

boolean

false

引用複合專案時,停用優先使用原始檔而非宣告檔案的設定。

--downlevelIteration

boolean

false

在迭代時,輸出更符合標準但更冗長且效能較低的 JavaScript 程式碼。

--emitBOM

boolean

false

在輸出檔案的開頭包含 UTF-8 位元組順序標記 (BOM)。

--emitDeclarationOnly

boolean

false

僅輸出 d.ts 檔案,不輸出 JavaScript 檔案。

--emitDecoratorMetadata

boolean

false

為原始檔中的裝飾器聲明發出設計型別元資料。

--erasableSyntaxOnly

boolean

false

不允許使用非 ECMAScript 標準的執行時構造。

--esModuleInterop

boolean

如果 modulenode16, nodenext, 或 preserve 則為 true;否則為 false

發出額外的 JavaScript 程式碼以簡化對 CommonJS 模組匯入的支援。這會啟用 allowSyntheticDefaultImports 以實現型別相容性。

--exactOptionalPropertyTypes

boolean

false

按字面意思解釋可選屬性型別,而不是新增 undefined

--experimentalDecorators

boolean

false

啟用對 TC39 第 2 階段草案裝飾器的實驗性支援。

--explainFiles

boolean

false

列印編譯期間讀取的檔案,包括它們被包含的原因。

--extendedDiagnostics

boolean

false

構建後輸出更詳細的編譯器效能資訊。

--forceConsistentCasingInFileNames

boolean

true

確保匯入中的大小寫正確。

--generateCpuProfile

string

profile.cpuprofile

生成編譯器執行的 v8 CPU 配置檔案以進行除錯。

--generateTrace

string

生成事件跟蹤和型別列表。

--importHelpers

boolean

false

允許每個專案從 tslib 匯入輔助函式一次,而不是每個檔案包含一份。

--importsNotUsedAsValues

remove, preserve, 或 error

remove

為僅用於型別的匯入指定輸出/檢查行為。

--incremental

boolean

如果開啟了 composite 則為 true;否則為 false

儲存 .tsbuildinfo 檔案以允許對專案進行增量編譯。

--inlineSourceMap

boolean

false

在發出的 JavaScript 中包含源對映檔案。

--inlineSources

boolean

false

在發出的 JavaScript 的源對映中包含原始碼。

--isolatedDeclarations

boolean

false

要求對匯出進行足夠的標註,以便其他工具可以輕鬆生成宣告檔案。

--isolatedModules

boolean

如果開啟了 verbatimModuleSyntax 則為 true;否則為 false

確保每個檔案都可以安全地轉譯,而無需依賴其他匯入。

--jsx

preserve, react, react-native, react-jsx, 或 react-jsxdev

指定生成什麼型別的 JSX 程式碼。

--jsxFactory

string

React.createElement

指定在目標為 React JSX 輸出時使用的 JSX 工廠函式,例如 'React.createElement' 或 'h'。

--jsxFragmentFactory

string

React.Fragment

指定在目標為 React JSX 輸出時用於片段的 JSX 片段引用,例如 'React.Fragment' 或 'Fragment'。

--jsxImportSource

string

react

指定使用 jsx: react-jsx* 時用於匯入 JSX 工廠函式的模組說明符。

--keyofStringsOnly

boolean

false

使 keyof 僅返回字串,而不是字串、數字或符號。遺留選項。

--lib

列表

指定一組描述目標執行時環境的捆綁庫宣告檔案。

--libReplacement

boolean

false

啟用用自定義檔案替換預設的 lib 檔案。

--listEmittedFiles

boolean

false

編譯後列印輸出的檔名。

--listFiles

boolean

false

列印編譯期間讀取的所有檔案。

--mapRoot

string

指定偵錯程式定位對映檔案的位置,而不是生成的位置。

--maxNodeModuleJsDepth

number

0

指定用於檢查 node_modules 中 JavaScript 檔案的最大資料夾深度。僅適用於 allowJs

--module

none, commonjs, amd, umd, system, es6/es2015, es2020, es2022, esnext, node16, node18, node20, nodenext, 或 preserve

如果 targetES5 則為 CommonJS;否則為 ES6/ES2015

指定生成什麼型別的模組程式碼。

--moduleDetection

legacy, auto, 或 force

"auto":將包含 import、export、import.meta、jsx (jsx: react-jsx) 或 esm 格式 (module: node16+) 的檔案視為模組。

指定用於檢測檔案是指令碼還是模組的方法。

--moduleResolution

classic, node10/node, node16, nodenext, 或 bundler

如果 moduleCommonJS 則為 Node10;如果 moduleNode16, Node18, 或 Node20 則為 Node16;如果 moduleNodeNext 則為 NodeNext;如果 modulePreserve 則為 Bundler;否則為 Classic

指定 TypeScript 如何根據給定的模組說明符查詢檔案。

--moduleSuffixes

列表

解析模組時要搜尋的檔名字尾列表。

--newLine

crlflf

lf

設定輸出檔案的換行符。

--noCheck

boolean

false

停用完整型別檢查(僅報告嚴重解析和輸出錯誤)。

--noEmit

boolean

false

停用從編譯中輸出檔案。

--noEmitHelpers

boolean

false

停用在編譯輸出中生成像 __extends 這樣的自定義輔助函式。

--noEmitOnError

boolean

false

如果報告了任何型別檢查錯誤,則停用輸出檔案。

--noErrorTruncation

boolean

false

停用錯誤訊息中的型別截斷。

--noFallthroughCasesInSwitch

boolean

false

啟用 switch 語句中 fallthrough 情況的錯誤報告。

--noImplicitAny

boolean

如果開啟了 strict 則為 true;否則為 false

對具有隱式 any 型別的表示式和宣告啟用錯誤報告。

--noImplicitOverride

boolean

false

確保派生類中重寫成員被標記為 override 修飾符。

--noImplicitReturns

boolean

false

對函式中沒有顯式返回的程式碼路徑啟用錯誤報告。

--noImplicitThis

boolean

如果開啟了 strict 則為 true;否則為 false

this 被賦予 any 型別時,啟用錯誤報告。

--noImplicitUseStrict

boolean

false

停用在發出的 JavaScript 檔案中新增 'use strict' 指令。

--noLib

boolean

false

停用包含任何庫檔案,包括預設的 lib.d.ts。

--noPropertyAccessFromIndexSignature

boolean

false

強制對使用索引型別宣告的鍵使用索引訪問器。

--noResolve

boolean

false

不允許 importrequire<reference> 擴充套件 TypeScript 應新增到專案中的檔案數量。

--noStrictGenericChecks

boolean

false

停用函式型別中泛型簽名的嚴格檢查。

--noUncheckedIndexedAccess

boolean

false

使用索引訪問型別時新增 undefined

--noUncheckedSideEffectImports

boolean

true

檢查副作用匯入。

--noUnusedLocals

boolean

false

當局部變數未被讀取時啟用錯誤報告。

--noUnusedParameters

boolean

false

當函式引數未被讀取時引發錯誤。

--out

string

已棄用的設定。請改用 outFile

--outDir

string

為所有輸出檔案指定輸出資料夾。

--outFile

string

指定將所有輸出捆綁到一個 JavaScript 檔案中的檔案。如果 declaration 為 true,也指定一個捆綁所有 .d.ts 輸出的檔案。

--paths

object

指定一組將匯入重新對映到附加查詢位置的條目。

--plugins

列表

指定要包含的語言服務外掛列表。

--preserveConstEnums

boolean

如果開啟了 isolatedModules 則為 true;否則為 false

停用刪除生成程式碼中的 const enum 宣告。

--preserveSymlinks

boolean

false

停用將符號連結解析為其實際路徑。這對應於 node 中的相同標誌。

--preserveValueImports

boolean

false

在 JavaScript 輸出中保留未使用的匯入值,否則這些值會被刪除。

--preserveWatchOutput

boolean

false

停用監視模式下控制檯的清屏操作。

--pretty

boolean

true

在 TypeScript 輸出中啟用顏色和格式化,使編譯器錯誤更易於閱讀。

--reactNamespace

string

React

指定呼叫 createElement 的物件。這僅在目標為 react JSX 輸出時適用。

--removeComments

boolean

false

停用輸出註釋。

--resolveJsonModule

boolean

false

啟用匯入 .json 檔案。

--resolvePackageJsonExports

boolean

moduleResolutionnode16, nodenext, 或 bundler 時為 true;否則為 false

解析包匯入時使用 package.json 的 'exports' 欄位。

--resolvePackageJsonImports

boolean

moduleResolutionnode16, nodenext, 或 bundler 時為 true;否則為 false

解析匯入時使用 package.json 的 'imports' 欄位。

--rewriteRelativeImportExtensions

boolean

false

在輸出檔案中,將相對匯入路徑中的 .ts, .tsx, .mts, 和 .cts 副檔名重寫為其對應的 JavaScript 等價項。

--rootDir

string

根據輸入檔案列表計算得出。

指定原始檔中的根資料夾。

--rootDirs

列表

根據輸入檔案列表計算得出。

解析模組時,允許將多個資料夾視為一個。

--skipDefaultLibCheck

boolean

false

跳過對隨 TypeScript 一起提供的 .d.ts 檔案的型別檢查。

--skipLibCheck

boolean

false

跳過對所有 .d.ts 檔案的型別檢查。

--sourceMap

boolean

false

為發出的 JavaScript 檔案建立源對映檔案。

--sourceRoot

string

指定偵錯程式查詢引用原始碼的根路徑。

--stableTypeOrdering

boolean

false

確保型別在不同編譯之間以穩定且確定性的順序排列。

--stopBuildOnErrors

boolean

在上游專案出錯時跳過構建下游專案。

--strict

boolean

true

啟用所有嚴格型別檢查選項。

--strictBindCallApply

boolean

如果開啟了 strict 則為 true;否則為 false

檢查 bind, call, 和 apply 方法的引數是否與原始函式匹配。

--strictBuiltinIteratorReturn

boolean

如果開啟了 strict 則為 true;否則為 false

內建迭代器例項化時 TReturn 型別為 undefined 而不是 any。

--strictFunctionTypes

boolean

如果開啟了 strict 則為 true;否則為 false

分配函式時,檢查確保引數和返回值是子型別相容的。

--strictNullChecks

boolean

如果開啟了 strict 則為 true;否則為 false

型別檢查時,考慮 nullundefined

--strictPropertyInitialization

boolean

如果開啟了 strict 則為 true;否則為 false

檢查類屬性是否已宣告但未在建構函式中設定。

--stripInternal

boolean

false

停用發出 JSDoc 註釋中帶有 @internal 的宣告。

--suppressExcessPropertyErrors

boolean

false

停用在建立物件字面量時報告多餘屬性錯誤。

--suppressImplicitAnyIndexErrors

boolean

false

當索引缺乏索引簽名的物件時,抑制 noImplicitAny 錯誤。

--target

es3, es5, es6/es2015, es2016, es2017, es2018, es2019, es2020, es2021, es2022, es2023, es2024, es2025, 或 esnext

如果 modulenode20 則為 es2023;如果 modulenodenext 則為 esnext;否則為 ES5

設定發出的 JavaScript 的語言版本,幷包含相容的庫宣告。

--traceResolution

boolean

false

記錄 moduleResolution 過程中使用的路徑。

--tsBuildInfoFile

string

.tsbuildinfo

儲存 .tsbuildinfo 增量構建資訊的檔案。

--typeRoots

列表

指定多個像 ./node_modules/@types 一樣起作用的資料夾。

--types

列表

指定要包含的型別包名稱,而無需在原始檔中引用它們。

--useDefineForClassFields

boolean

如果 targetES2022 或更高版本(包括 ESNext)則為 true;否則為 false

發出符合 ECMAScript 標準的類欄位。

--useUnknownInCatchVariables

boolean

如果開啟了 strict 則為 true;否則為 false

將 catch 子句變數預設為 unknown 而不是 any

--verbatimModuleSyntax

boolean

false

不對未標記為 type-only 的匯入或匯出進行轉換或省略,確保它們根據 'module' 設定以輸出檔案格式書寫。

TypeScript 文件是一個開源專案。透過 傳送 Pull Request 幫助我們改進這些頁面 ❤

此頁面的貢獻者
MHMohamed Hegazy (96)
OTOrta Therox (54)
DRDaniel Rosenwasser (19)
ADAxel D (14)
JBJake Bailey (11)
65+

最後更新:2026 年 3 月 27 日