参考网址:TypeScript: Documentation - What is a tsconfig.json (typescriptlang.org)
TypeScript: TSConfig Reference - Docs on every TSConfig option (typescriptlang.org)
一、去掉注释
tsconfig.json初始是这样的
// "removeComments": true,
1、把注释取消掉
"removeComments": true,
2、运行tsc命令
tsc
生成的JS文件中没有注释
"removeComments": true,中true改为false,则不移除注释
3、tsc filename这个命令不会调用配置文件,所以这个命令生成的JS文件是默认配置
二、编译指定文件
在tsconfig.json中增加配置
"include": ["./index.ts"],
"compilerOptions": {
...
}
tsc命令只编译index.ts文件,要增加文件在数组中添加即可
如:
"include": ["./index.ts","./a.tx"],
"compilerOptions": {
...
}
三、不编译指定文件
"exclude": ["./index.ts","./a.tx"],
"compilerOptions": {
...
}
四、显示指定any
"noImplicitThis": true,
Implicit---->adj. 不言明[含蓄]的; 无疑问的,绝对的; 成为一部份的; 内含的;
五、null类型只能赋值给Object
"strictNullChecks": true,
报错:error TS2322: Type 'null' is not assignable to type 'string'
strictNullChecks为true时,null不能赋值给其他类型
六、把指定源目录中的TS文件编译到指定目标目录中
指定TS源目录
"rootDir": "./src", /* Specify the root folder within your source files. */
指定JS目标目录
"outDir": "./build", /* Specify an output folder for all emitted files. */
七、只编译新内容,渐进式编译/增量式编译
"incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */
设置为true后运行tsc会产生一个文件tsconfig.tsbuildinfo,里面记录了上一次编译的具体信息,下一次编译时会对照这个文件,只编译新内容
八、允许编译JS文件
"allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */
设置为true后,源目录中的JS文件也会被编译
九、检查JS文件的语法
"checkJs": true, /* Enable error reporting in type-checked JavaScript files. */
十、编译时生成.map文件
"sourceMap": true, /* Create source map files for emitted JavaScript files. */
十一、从未使用的局部变量报错
"noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */
十二、从未使用的函数参数报错
"noUnusedParameters": true,/* Raise an error when a function parameter isn't read. */