tsconfig.json 文件的生成
tsconfig.json 文件是由 tsc --init 命令生成的。
直接运行 tsc 命令 tsconfig.json 文件才会生效,要是指定文件比如 tsc dome.ts ,tsconfig.json 文件不会生效。但是 ts-node 指定文件是遵循 tsconfig.json 文件的。
include、exclude 和 files 配置项
// 编译哪些文件
{
"include":["demo.ts"],
"compilerOptions": {
//any something
//........
}
}
// 不编译哪些文件
{
"exclude":["demo2.ts"],
"compilerOptions": {
//any something
//........
}
}
// 和 include 差不多,都是编译哪些文件
{
"files":["demo.ts"],
"compilerOptions": {
//any something
//........
}
}
compilerOptions 配置项
不用特意去死记,因为并不常用,用的时候查 API 就可以了:编译选项详解
几种常用的编译选项:
| 配置项 | 作用 |
|---|---|
removeComments |
编译出来的js文件是否移除注释,为 true,就是在js中不显示注释 |
strict |
按照 TypeScript 最严格的规范来写代码 |
"outDir": "./build" |
编译输出的目录 |
"rootDir": "./src" |
ts 文件目录 |
"allowJs":true"target":'es5' |
编译 ES6 语法到 ES5 语法 |
sourceMap |
Source map 就是一个信息文件,里面储存着位置信息。也就是说,转换后的代码的每一个位置,所对应的转换前的位置 |
"noUnusedLocals":true |
编译时发现未使用的局部变量,会报错 |
"noUnusedParameters":true |
编译时发现未使用的方法,会报错 |
以下是 strict 为 false 或不配置 strict 配置项时,可以配置的配置项:
| 配置项 | 作用 |
|---|---|
noImplicitAny |
值为 false 时,允许注解类型 any 不用特意表明 |
strictNullChecks |
值为 false 时,不强制检查 NULL 类型 |
参考:
技术胖——TypeScript从入门到精通(15. 配置文件-初识 tsconfig.json)
技术胖——TypeScript从入门到精通(16. 配置文件-初识 compilerOptions配置项)
技术胖——TypeScript从入门到精通(17. 配置文件-compilerOptions 配置内容详解)
compilerOptions 编译选项详解
