Skip to content
赞助商
虚位以待
虚位以待
虚位以待
虚位以待
虚位以待
虚位以待
虚位以待
虚位以待
虚位以待
虚位以待
虚位以待
虚位以待
虚位以待
虚位以待
虚位以待
虚位以待
虚位以待

tsc CLI 选项 ​

使用 CLI ​

本地运行 tsc 将编译由 tsconfig.json 定义的最近的项目,或者你可以通过传入一组文件 glob 来编译一组 TypeScript 文件。当在命令行上指定输入文件时,将忽略 tsconfig.json 文件。

sh
# 基于向后查找文件系统中的 tsconfig.json 进行编译
tsc

# 仅使用编译器默认值编译 index.ts
tsc index.ts

# 使用默认设置编译文件夹 src 中的任何 .ts 文件
tsc src/*.ts

# 使用 tsconfig.production.json 中的编译器设置编译引用的文件
tsc --project tsconfig.production.json

# 为 js 文件输出 d.ts 文件,并显示布尔类型的编译器选项
tsc index.js --declaration --emitDeclarationOnly

# 通过接受字符串参数的编译器选项从两个文件输出一个 .js 文件
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

list

从监视过程中移除一个目录列表。

--excludeFiles

list

从监视模式处理中移除一个文件列表。

--fallbackPolling

fixedinterval, priorityinterval, dynamicpriority, or fixedchunksize

指定当系统用完原生文件监视器时,监视器应使用哪种方法。

--synchronousWatchDirectory

boolean

在不原生支持递归监视的平台上,同步调用回调并更新目录监视器的状态。

--watch

boolean

监视输入文件。

--watchDirectory

usefsevents, fixedpollinginterval, dynamicprioritypolling, or fixedchunksizepolling

指定在缺乏递归文件监视功能的系统上如何监视目录。

--watchFile

fixedpollinginterval, prioritypollinginterval, dynamicprioritypolling, fixedchunksizepolling, usefsevents, or usefseventsonparentdirectory

指定 TypeScript 监视模式的工作方式。

编译器标志

标志类型默认值
--allowArbitraryExtensions

boolean

false

允许导入任何扩展名的文件,前提是存在声明文件。

--allowImportingTsExtensions

boolean

true 如果 rewriteRelativeImportExtensions;否则 false。

允许导入包含 TypeScript 文件扩展名。

--allowJs

boolean

false,除非设置了 checkJs

允许 JavaScript 文件成为程序的一部分。使用 checkJS 选项从这些文件中获取错误。

--allowSyntheticDefaultImports

boolean

true 如果 esModuleInterop 启用,module 是 system,或 moduleResolution 是 bundler;否则 false。

当模块没有默认导出时,允许 'import x from y'。

--allowUmdGlobalAccess

boolean

false

允许从模块访问 UMD 全局变量。

--allowUnreachableCode

boolean

禁止报告无法访问的代码错误。

--allowUnusedLabels

boolean

禁止报告未使用的标签错误。

--alwaysStrict

boolean

true 如果 strict;否则 false。

确保始终输出 'use strict'。

--assumeChangesOnlyAffectDirectDependencies

boolean

false

在使用 incremental 和 watch 模式的项目中,假设文件中的更改只会影响直接依赖于它的文件。

--baseUrl

string

指定解析裸说明符模块名称的基本目录。

--charset

string

utf8

不再支持。在早期版本中,手动设置读取文件的文本编码。

--checkJs

boolean

false

在类型检查的 JavaScript 文件中启用错误报告。

--composite

boolean

false

启用允许 TypeScript 项目与项目引用一起使用的约束。

--customConditions

list

在解析导入时,除了解析器特定的默认值之外要设置的条件。

--declaration

boolean

true 如果 composite;否则 false。

从项目中的 TypeScript 和 JavaScript 文件生成 .d.ts 文件。

--declarationDir

string

指定生成的声明文件的输出目录。

--declarationMap

boolean

false

为 d.ts 文件创建源映射。

--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

true 如果 module 是 node16、nodenext 或 preserve;否则 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 profile 用于调试。

--generateTrace

string

生成事件跟踪和类型列表。

--importHelpers

boolean

false

允许从 tslib 每个项目导入一次辅助函数,而不是每个文件都包含它们。

--importsNotUsedAsValues

remove, preserve, or error

remove

指定仅用于类型的导入的输出/检查行为。

--incremental

boolean

true 如果 composite;否则 false。

保存 .tsbuildinfo 文件以允许项目的增量编译。

--inlineSourceMap

boolean

false

在输出的 JavaScript 中包含源映射文件。

--inlineSources

boolean

false

在输出的 JavaScript 的源映射中包含源代码。

--isolatedDeclarations

boolean

false

要求导出有足够的注解,以便其他工具可以轻松生成声明文件。

--isolatedModules

boolean

true 如果 verbatimModuleSyntax;否则 false。

确保每个文件可以安全地转译,而无需依赖其他导入。

--jsx

preserve, react, react-native, react-jsx, or 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

list

指定一组描述目标运行时环境的捆绑库声明文件。

--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, or preserve

CommonJS 如果 target 是 ES5;否则 ES6/ES2015。

指定生成的模块代码。

--moduleDetection

legacy, auto, or force

"auto": 将具有导入、导出、import.meta、jsx(使用 jsx: react-jsx)或 esm 格式(使用 module: node16+)的文件视为模块。

指定用于检测文件是脚本还是模块的方法。

--moduleResolution

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

Node10 如果 module 是 CommonJS;Node16 如果 module 是 Node16、Node18 或 Node20;NodeNext 如果 module 是 NodeNext;Bundler 如果 module 是 Preserve;否则 Classic。

指定 TypeScript 如何根据给定的模块说明符查找文件。

--moduleSuffixes

list

解析模块时要搜索的文件名后缀列表。

--newLine

crlf or lf

lf

设置输出文件的换行符。

--noCheck

boolean

false

禁用完整的类型检查(仅报告关键的解析和输出错误)。

--noEmit

boolean

false

禁止从编译输出文件。

--noEmitHelpers

boolean

false

禁止在编译输出中生成像 __extends 这样的自定义辅助函数。

--noEmitOnError

boolean

false

如果报告任何类型检查错误,则禁止输出文件。

--noErrorTruncation

boolean

false

禁止在错误消息中截断类型。

--noFallthroughCasesInSwitch

boolean

false

对 switch 语句中的贯穿 case 启用错误报告。

--noImplicitAny

boolean

true 如果 strict;否则 false。

对带有隐含 any 类型的表达式和声明启用错误报告。

--noImplicitOverride

boolean

false

确保派生类中覆盖的成员使用 override 修饰符标记。

--noImplicitReturns

boolean

false

对函数中未显式返回的代码路径启用错误报告。

--noImplicitThis

boolean

true 如果 strict;否则 false。

当 this 被赋予类型 any 时启用错误报告。

--noImplicitUseStrict

boolean

false

禁止在输出的 JavaScript 文件中添加 'use strict' 指令。

--noLib

boolean

false

禁用包含任何库文件,包括默认的 lib.d.ts。

--noPropertyAccessFromIndexSignature

boolean

false

强制对使用索引类型声明的键使用索引访问器。

--noResolve

boolean

false

禁止 import、require 或 <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

list

指定要包含的语言服务插件列表。

--preserveConstEnums

boolean

true 如果 isolatedModules;否则 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

true 当 moduleResolution 是 node16、nodenext 或 bundler 时;否则 false

在解析包导入时使用 package.json 的 'exports' 字段。

--resolvePackageJsonImports

boolean

true 当 moduleResolution 是 node16、nodenext 或 bundler 时;否则 false

在解析导入时使用 package.json 的 'imports' 字段。

--rewriteRelativeImportExtensions

boolean

false

在相对导入路径中将 .ts、.tsx、.mts 和 .cts 文件扩展名重写为输出文件中对应的 JavaScript 扩展名。

--rootDir

string

根据输入文件列表计算。

指定源文件中的根文件夹。

--rootDirs

list

根据输入文件列表计算。

允许将多个文件夹视为一个,以解析模块。

--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

true 如果 strict;否则 false。

检查 bind、call 和 apply 方法的参数是否与原始函数匹配。

--strictBuiltinIteratorReturn

boolean

true 如果 strict;否则 false。

内置迭代器使用 undefined 而不是 any 作为 TReturn 类型实例化。

--strictFunctionTypes

boolean

true 如果 strict;否则 false。

分配函数时,检查以确保参数和返回值是子类型兼容的。

--strictNullChecks

boolean

true 如果 strict;否则 false。

进行类型检查时,考虑 null 和 undefined。

--strictPropertyInitialization

boolean

true 如果 strict;否则 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, or esnext

es2023 如果 module 是 node20;esnext 如果 module 是 nodenext;否则 ES5。

设置输出 JavaScript 的 JavaScript 语言版本,并包含兼容的库声明。

--traceResolution

boolean

false

记录在 moduleResolution 过程中使用的路径。

--tsBuildInfoFile

string

.tsbuildinfo

存储 .tsbuildinfo 增量构建信息的文件。

--typeRoots

list

指定多个充当 ./node_modules/@types 的文件夹。

--types

list

指定要包含的类型包名称,而无需在源文件中引用。

--useDefineForClassFields

boolean

true 如果 target 是 ES2022 或更高版本,包括 ESNext;否则 false。

输出符合 ECMAScript 标准的类字段。

--useUnknownInCatchVariables

boolean

true 如果 strict;否则 false。

默认将 catch 子句变量设置为 unknown 而不是 any。

--verbatimModuleSyntax

boolean

false

不转换或省略任何未标记为仅类型的导入或导出,确保它们根据 'module' 设置以输出文件的格式写入。

相关 ​