命令行中编译选项
选项
描述
-w/–watch
在监视模式下运行编译器
tsConfig.json tsConfig.json 是 ts 编译器的配置文件,ts 编译器将根据 tsConfig.json 中的配置信息对代码进行编译。
根目录下存在 tsConfig.json
, terminal 中运行以下命令行,将编译当前目录及子目录中的所有 ts 文件
配置介绍 include include
指定需要编译的文件路径
1 2 3 4 5 6 7 { "include" : [ "./src/**/*" , "..." ], }
exclude exclude
指定不需要编译的文件路径exclude
默认配置为 ["node_modules", "bower_components","jspm_packages"]
1 2 3 4 5 6 7 8 9 10 { "include" : [ "./src/**/*" , "..." ], "exclude" : [ "./src/compiled/*" ] }
extends extends
指定需要继承的配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 { "include" : [ "./src/**/*" , "..." ], "exclude" : [ "./src/compiled/*" ], "extends" : [ "../config.json" ] }
files files
具体指明编译的文件列表只有需要编译的文件少时才会用到
1 2 3 4 5 6 { "files" : [ "./index.ts" , "./app.ts" ] }
compilerOptions ts 编译器选项
target
要将 ts 编译为 ES 的哪个版本
1 2 3 4 5 6 7 8 { "files" : [ "./variable/01.ts" ], "compilerOptions" : { "target" : "ES2020" } }
module
指定使用的模块化规范,【更多关于模块化】
1 2 3 4 5 6 { "compilerOptions" : { "target" : "es6" , "module" : "es6" } }
lib
指定项目中要使用的库Cannot find name ‘console’. Do you need to change your target library? Try changing the ‘lib’ compiler option to include ‘dom’.
1 2 3 4 5 6 7 { "compilerOptions" : { lib : ["es6" , "dom" ] } }
outDir
指定编译后文件的存储目录
1 2 3 4 5 { "compilerOptions" : { "outDir" : "./dist" } }
outFile
将代码合并为一个文件设置 outFile
扣,所有的全局作用域中的代码会合并到同一个文件中 Only ‘amd’ and ‘system’ modules are supported alongside –outFile.
1 2 3 4 5 6 { "compilerOptions" : { "module" : "amd" , "outFile" : "./dist/outFile.js" } }
allowJs
是否编译 js 文件,默认 false 不编译;checkJs
是否对 js 文件进行 ts 语法检查,默认不检查
1 2 3 4 5 6 7 8 { "compilerOptions" : { "module" : "amd" , "outFile" : "./dist/outFile.js" , "allowJs" : true , "checkJs" : true , } }
removeComments
编译时移除注释
noEmit
不生成编译后的文件 使用频率低,语法检查时使用
noEmitOnError
编译错误时不生成编译后文件
1 2 3 4 5 6 7 8 9 { "compilerOptions" : { "module" : "amd" , "outFile" : "./dist/outFile.js" , "removeComments" : true , "noEmitOnError" : true , "noEmit" : true } }
noImplicitAny
不允许使用隐匿 any
类型
1 2 3 4 5 6 7 let fn = function (a, b ) { return a + b }
noImplicitAny
开启
1 2 3 4 5 6 7 8 9 10 11 { "compilerOptions" : { "module" : "amd" , "outFile" : "./dist/outFile.js" , "removeComments" : true , "noEmitOnError" : true , "noEmit" : true , "noImplicitAny" : true } }
Error:
1 2 3 4 5 6 let fn = function (a, b ) { return a + b }
noImplicitAny
不允许不明确类型的this {noImplicitAny:true}
1 2 3 4 5 6 7 8 9 10 11 12 let fn = function ( ) { console .log (this ) } let fn = function (this : any ) { console .log (this ) }
strictNullChecks
检查语法是否存在空值
1 2 3 4 5 6 7 8 9 10 11 12 { "compilerOptions" : { "module" : "amd" , "outFile" : "./dist/outFile.js" , "removeComments" : true , "noEmitOnError" : true , "noEmit" : true , "noImplicitAny" : true , "strictNullChecks" : true } }
1 2 let box = document .getElementById ('box' )box.innerHtml = '<div>null</div>'
TS2531: Object is possibly ‘null’
1 2 let box = document .getElementById ('box' )box?.innerHtml = '<div>null</div>'
strict
所有严格检查的总开关