简介:本文将详细解析Typescript编译器tsc的基础用法和常用参数,帮助你更好地理解和使用Typescript。
在TypeScript中,tsc是TypeScript编译器的命令行工具。它负责将TypeScript代码转换为JavaScript代码,以便在浏览器或Node.js环境中运行。下面我们将解析tsc命令的常用参数和用法。
常用参数
-v
或 --version
: 显示tsc的版本信息。-w
或 --watch
: 监视文件变化并自动编译。-p
或 --project
: 指定tsconfig.json文件的路径。-t
或 --target
: 设置编译目标版本,如ES3、ES5、ES6等。-m
或 --module
: 设置模块系统,如commonjs、amd、system等。-d
或 --declaration
: 生成类型声明文件(.d.ts)。-o
或 --outFile
: 将输出文件合并到一个单一的文件中。-I
或 --include
: 指定要包含的文件或文件夹。-L
或 --lib
: 指定要包含的库文件。-r
或 --rootDir
: 指定源文件的根目录。-T
或 --noEmit
: 不输出文件,仅检查语法错误。-P
或 --parserOptionsFile
: 指定解析器选项文件的路径。-D
或 --noEmitOnError
: 在出现错误时不输出文件。-C
或 --charset
: 设置源文件的字符集编码。-TSTargetNode60
: 将目标Node.js版本设置为6.0。
tsc -p ./tsconfig.json
tsc -w
tsc file1.ts file2.ts
tsc -d --target ES5 --module commonjs file1.ts
tsc -o output.js --target ES6 --module amd file1.ts file2.ts
tsc -I include/file1.ts include/folder/*.ts
tsc --exclude exclude/file1.ts exclude/folder/*.ts
tsc -P options.json --target ES6 --module commonjs file1.ts
tsc -TTargetNode60 --target ES6 --module commonjs file1.ts file2.ts
tsc -rsrc/ -o output/ --noEmitOnError --target ES6 --module commonjs src/**/*.ts