TS_编译选项_续

68 阅读1分钟

compilerOptions 编译器的选项

1、allowJs (是否对js文件进行编译)

默认值: false
可选值: true、false
类型: boolean

"compilerOptions":{ 
    "allowJs":true 
}



// 如果src下有.js文件,则会被编译
2、checkJs (是否检查.js文件的代码 类型)

默认值: false
可选值: true、false
类型: boolean

"compilerOptions":{ 
// 如果 checkJs 设置为true,则相应地 allowJs 也要设置为true
     "allowJs":true ,
     
    "checkJs":true 
}

//app.js
let hi = "hi";
hi = 123;    //不能将类型“number”分配给类型“string”

3、removeComments (是否移除注释)

默认值: false
可选值: true、false
类型: boolean

"compilerOptions":{ 
    "removeComments":true
 } 
 //编译前的app.ts 文件 
 //我是一个注释
 const a = 3; 
 
 //编译后的app.ts 文件 
 var a = 3; 

设置为false 的话

"compilerOptions":{ 
    "removeComments":false
 } 
 //编译前的app.ts 文件 
 //我是一个注释
 const a = 3; 
 
 //编译后的app.ts 文件 
 //我是一个注释
 var a = 3; 
4、noEmit (不生成编译文件)

默认值: false
可选值: true、false
类型: boolean

设置为true后,对文件进行编译,但不会生成编译后的文件

4、noEmitOnError (当编译时有错误时,不生成编译文件)

默认值: false
可选值: true、false
类型: boolean

设置为true后,对文件进行编译,如果出现类型错误,则不会生成编译后的文件,否则生成编译文件

"compilerOptions":{ 
    "noEmitOnError":true 
}

//app.ts
let hi = "hi";
hi = 123;    //Type 'string' is not assignable to type 'number'.

//这时候编译会失败,并不生成编译文件