tsc CLI 选项

使用 CLI

¥Using the CLI

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

¥Running tsc locally will compile the closest project defined by a tsconfig.json, or you can compile a set of TypeScript files by passing in a glob of files you want. When input files are specified on the command line, tsconfig.json files are ignored.

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

编译器选项

¥Compiler Options

如果你正在寻找有关 tsconfig 中的编译器选项的更多信息,请查看 TSConfig 参考手册

¥If you’re looking for more information about the compiler options in a tsconfig, check out the TSConfig Reference

CLI 命令

标志 类型
--all

boolean

显示所有编译器选项。

--generateTrace

string

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

--help

boolean

提供本地信息以获取有关 CLI 的帮助。

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

fixedintervalpriorityintervaldynamicpriorityfixedchunksize

指定当系统用完原生文件观察程序时观察程序应使用的方法。

--synchronousWatchDirectory

boolean

在原生不支持递归观察的平台上同步调用回调并更新目录观察者的状态。

--watch

boolean

监视输入文件。

--watchDirectory

usefseventsfixedpollingintervaldynamicprioritypollingfixedchunksizepolling

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

--watchFile

fixedpollingintervalprioritypollingintervaldynamicprioritypollingfixedchunksizepollingusefseventsusefseventsonparentdirectory

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

编译器标志

标志 类型 默认
--allowArbitraryExtensions

boolean

false

如果存在声明文件,则允许导入具有任何扩展名的文件。

--allowImportingTsExtensions

boolean

false

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

--allowJs

boolean

false

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

--allowSyntheticDefaultImports

boolean

如果 esModuleInterop 使能,则 truemodulesystem,或 moduleResolutionbundler;否则为 false

当模块没有默认导出时允许 '从 y 导入 x'。

--allowUmdGlobalAccess

boolean

false

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

--allowUnreachableCode

boolean

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

--allowUnusedLabels

boolean

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

--alwaysStrict

boolean

如果是 strict,则为 true;否则为 false

确保始终触发 '使用严格'。

--assumeChangesOnlyAffectDirectDependencies

boolean

false

在使用 incrementalwatch 模式的项目中进行重新编译假设文件内的更改只会影响直接依赖于它的文件。

--baseUrl

string

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

--charset

string

utf8

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

--checkJs

boolean

false

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

--composite

boolean

false

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

--customConditions

list

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

--declaration

boolean

如果是 composite,则为 true;否则为 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

为源文件中的修饰声明触发设计类型元数据。

--esModuleInterop

boolean

如果 modulenode16nodenext,则为 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 配置文件以进行调试。

--importHelpers

boolean

false

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

--importsNotUsedAsValues

removepreserveerror

remove

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

--incremental

boolean

如果是 composite,则为 true;否则为 false

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

--inlineSourceMap

boolean

false

在触发的 JavaScript 中包含 sourcemap 文件。

--inlineSources

boolean

false

在触发的 JavaScript 内的源映射中包含源代码。

--isolatedModules

boolean

false

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

--jsx

preservereactreact-nativereact-jsxreact-jsxdev

指定生成的 JSX 代码。

--jsxFactory

string

React.createElement

指定针对 React JSX 触发时使用的 JSX 工厂函数,例如 'React.createElement' 或 'h'。

--jsxFragmentFactory

string

React.Fragment

指定针对 React JSX 触发时用于片段的 JSX 片段引用,例如 'React.Fragment' 或 '分段'。

--jsxImportSource

string

react

指定使用 jsx: react-jsx* 时用于导入 JSX 工厂函数的模块说明符。

--keyofStringsOnly

boolean

false

使 keyof 只返回字符串而不是字符串、数字或符号。旧版选项。

--lib

list

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

--listEmittedFiles

boolean

false

编译后打印触发文件的名称。

--listFiles

boolean

false

打印编译期间读取的所有文件。

--mapRoot

string

指定调试器应该定位映射文件而不是生成的位置的位置。

--maxNodeModuleJsDepth

number

XSPACE0

指定用于从 node_modules 检查 JavaScript 文件的最大文件夹深度。仅适用于 allowJs

--module

nonecommonjsamdumdsystemes6/es2015es2020es2022esnextnode16nodenext

如果 targetES3ES5,则为 CommonJS;否则为 ES6/ES2015

指定生成什么模块代码。

--moduleDetection

legacyautoforce

"auto":使用导入、导出、import.meta、jsx(使用 jsx:react-jsx)或 esm 格式(使用模块:node16+)作为模块。

指定使用什么方法来检测文件是脚本还是模块。

--moduleResolution

classicnode10/nodenode16nodenextbundler

如果 moduleAMDUMDSystemES6/ES2015,则 Classic;如果 modulenode16nodenext,则匹配;否则匹配 Node

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

--moduleSuffixes

list

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

--newLine

crlflf

特定于平台。

设置用于触发文件的换行符。

--noEmit

boolean

false

禁用从编译中触发文件。

--noEmitHelpers

boolean

false

禁用在编译输出中生成自定义辅助函数(例如 __extends)。

--noEmitOnError

boolean

false

如果报告任何类型检查错误,则禁用触发文件。

--noErrorTruncation

boolean

false

禁用错误消息中的截断类型。

--noFallthroughCasesInSwitch

boolean

false

在 switch 语句中启用失败案例的错误报告。

--noImplicitAny

boolean

如果是 strict,则为 true;否则为 false

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

--noImplicitOverride

boolean

false

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

--noImplicitReturns

boolean

false

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

--noImplicitThis

boolean

如果是 strict,则为 true;否则为 false

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

--noImplicitUseStrict

boolean

false

禁止在触发的 JavaScript 文件中添加 '使用严格' 指令。

--noLib

boolean

false

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

--noPropertyAccessFromIndexSignature

boolean

false

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

--noResolve

boolean

false

禁止 importrequire<reference> 扩展 TypeScript 应添加到项目中的文件数量。

--noStrictGenericChecks

boolean

false

禁用对函数类型中的泛型签名的严格检查。

--noUncheckedIndexedAccess

boolean

false

使用索引访问时将 undefined 添加到类型。

--noUnusedLocals

boolean

false

未读取局部变量时启用错误报告。

--noUnusedParameters

boolean

false

未读取函数参数时引发错误。

--out

string

已弃用的设置。请改用 outFile

--outDir

string

为所有触发的文件指定一个输出文件夹。

--outFile

string

指定一个文件,将所有输出打包到一个 JavaScript 文件中。如果 declaration 为 true,还指定一个打包所有 .d.ts 输出的文件。

--paths

object

指定一组将导入重新映射到其他查找位置的条目。

--plugins

list

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

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

moduleResolutionnode16nodenextbundlertrue;否则 false

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

--resolvePackageJsonImports

boolean

moduleResolutionnode16nodenextbundlertrue;否则 false

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

--rootDir

string

根据输入文件列表计算。

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

--rootDirs

list

根据输入文件列表计算。

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

--skipDefaultLibCheck

boolean

false

跳过 TypeScript 中包含的类型检查 .d.ts 文件。

--skipLibCheck

boolean

false

跳过类型检查所有 .d.ts 文件。

--sourceMap

boolean

false

为触发的 JavaScript 文件创建源映射文件。

--sourceRoot

string

指定调试器查找参考源代码的根路径。

--strict

boolean

false

启用所有严格的类型检查选项。

--strictBindCallApply

boolean

如果是 strict,则为 true;否则为 false

检查 bindcallapply 方法的参数是否与原始函数匹配。

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

es3es5es6/es2015es2016es2017es2018es2019es2020es2021es2022esnext

ES3

为触发的 JavaScript 设置 JavaScript 语言版本并包含兼容的库声明。

--traceResolution

boolean

false

moduleResolution 进程期间使用的日志路径。

--tsBuildInfoFile

string

.tsbuildinfo

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

--typeRoots

list

指定多个文件夹,其作用类似于 ./node_modules/@types

--types

list

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

--useDefineForClassFields

boolean

如果 targetES2022 或更高,则为 true,否则包括 ESNextfalse

触发符合 ECMAScript 标准的类字段。

--useUnknownInCatchVariables

boolean

如果是 strict,则为 true;否则为 false

默认 catch 子句变量为 unknown 而不是 any

--verbatimModuleSyntax

boolean

false

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

相关内容

¥Related