【转存】tsconfig各项意义

自定义的一个tsconfig.json

1
2
3
4
5
6
7
8
9
10
11
12
13
{
"compilerOptions": {
"incremental": true,
"tsBuildInfoFile": "build/bulidFile",
"diagnostics": true,
"target": "ES6",
"module": "ES6",
"outDir": "dist"
},
"include": [
"src/**/*"
]
}

一个简单的 tsconfig.json 配置文件会有如下结构:

1
2
3
4
5
6
7
8
9
{
"compilerOptions": {

},
"files": [
"app.ts",
"foo.ts",
]
}

其中, ```compilerOptions```用来配置编译选项,files是一个数组,用来指定待编译文件。 这里的待编译文件是指入口文件,任何被入口文件依赖的文件,比如 ```foo.ts```依赖```bar.ts```,那这里并不需要写上```bar.ts```,编译器会自动把所有的依赖文件纳为编译对象。 也可以使用 ``` include ```和 ```exclude``` 来指定和排除待编译文件:

1
2
3
4
5
6
7
8
9
10
11
{
"compilerOptions": {
},
"include": [
"src/**/*"
],
"exclude": [
"node_modules",
"**/*.spec.ts"
]
}
  • 编译选项配置 compilerOptions:配置编译选项
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"incremental": true, // TS编译器在第一次编译之后会生成一个存储编译信息的文件,第二次编译会在第一次的基础上进行增量编译,可以提高编译的速度
"tsBuildInfoFile": "./buildFile", // 增量编译文件的存储位置
"diagnostics": true, // 打印诊断信息
"target": "ES5", // 目标语言的版本
"module": "CommonJS", // 生成代码的模板标准
// 默认值 target === "es3" or "es5" ?"commonjs" : "es6"
"outFile": "./app.js", // 将多个相互依赖的文件生成一个文件,可以用在AMD模块中,
// 即开启时应设置"module": "AMD",
"lib": [], // 编译时引入的 ES 功能库,包括:es5 、es6、es7、dom 等。// 如果未设置,则默认为: target 为 es5 时: ["dom", "es5", "scripthost"]
//target 为 es6 时: ["dom", "es6", "dom.iterable", "scripthost"]
"allowJS": true, // 允许编译器编译JS,JSX文件
"checkJs": true, // 允许在JS文件中报错,通常与allowJS一起使用
"outDir": "./dist", // 指定输出目录
"rootDir": "./", // 指定输出文件目录(用于输出),用于控制输出目录结构
"declaration": true, // 生成声明文件,开启后会自动生成声明文件
"declarationDir": "./file", // 指定生成声明文件存放目录
"emitDeclarationOnly": true, // 只生成声明文件,而不会生成js文件
"sourceMap": true, // 生成目标文件的sourceMap文件
"inlineSourceMap": true, // 生成目标文件的inline SourceMap,//inline SourceMap会包含在生成的js文件中
"declarationMap": true, // 为声明文件生成sourceMap
"typeRoots": [], // 声明文件目录,默认时node_modules/@types
"types": [], // 加载的声明文件包
//如果指定了某个值, 她会在 typeRoots 下找这个包,找到了就只加载这个包
"removeComments":true, // 删除注释
"noEmit": true, // 不输出文件,即编译后不会生成任何js文件
"noEmitOnError": true, // 发送错误时不输出任何文件
"noEmitHelpers": true, // 不生成helper函数,减小体积,需要额外安装,常配合importHelpers一起使用
"importHelpers": true, // 通过tslib引入helper函数,文件必须是模块
"downlevelIteration": true, // 降级遍历器实现,如果目标源是es3/5,那么遍历器会有降级的实现
"strict": true, // 开启所有严格的类型检查
"alwaysStrict": true, // 在代码中注入'use strict'
"noImplicitAny": true, // 不允许隐式的any类型
"strictNullChecks": true, // 不允许把null、undefined赋值给其他类型的变量
"strictFunctionTypes": true, // 不允许函数参数双向协变
"strictPropertyInitialization": true, // 类的实例属性必须初始化
"strictBindCallApply": true, // 严格的bind/call/apply检查
"noImplicitThis": true, // 不允许this有隐式的any类型
"noUnusedLocals": true, // 检查只声明、未使用的局部变量(只提示不报错)
"noUnusedParameters": true, // 检查未使用的函数参数(只提示不报错)
"noFallthroughCasesInSwitch": true, // 防止switch语句贯穿(即如果没有break语句后面不会执行)
"noImplicitReturns": true, //每个分支都会有返回值
"esModuleInterop": true, // 允许export=导出,由import from 导入
"allowUmdGlobalAccess": true, // 允许在模块中全局变量的方式访问umd模块
"moduleResolution": "node", // 模块解析策略,ts默认用node的解析策略,即相对的方式导入
"baseUrl": "./", // 解析非相对模块的基地址,默认是当前目录
"paths": { // 路径映射,相对于baseUrl
// 如使用jq时不想使用默认版本,而需要手动指定版本,可进行如下配置
"jquery": ["node_modules/jquery/dist/jquery.min.js"]
},
"rootDirs": ["src","out"], // 将多个目录放在一个虚拟目录下,用于运行时,
//即编译后引入文件的位置可能发生变化,
//这也设置可以虚拟src和out在同一个目录下,不用再去改变路径也不会报错
"listEmittedFiles": true, // 打印输出文件
"listFiles": true , // 打印编译的文件(包括引用的声明文件)
"jsx":"Preserve" //在 .tsx 中支持 JSX :React 或 Preserve
"jsxFactory":"" //默认值 React.createElement 。 jsx 设置为 React 时使用的创建函数
```

typeRoots和types

声明模块通常会包含一个 index.d.ts 文件,或者其 package.json 设置了 types 字段。

默认的,所有位于 node_modules/@types 路径下的模块都会引入到编译器。
具体来说是,./node_modules/@types 、../node_modules/@types、../../node_modules/@types 等等。

typeRoots 用来指定默认的类型声明文件查找路径,默认为 node_modules/@types 。比如:
```js
{
"compilerOptions": {
"typeRoots": ["./typings"]
}
}

上面的配置会自动引入 ./typings 下的所有 TS 类型声明模块,而不是 ./node_modules/@types 下的模块。 如果不希望自动引入 typeRoots 指定路径下的所有声明模块,那可以使用 types 指定自动引入哪些模块。比如:

1
2
3
4
5
{
"compilerOptions": {
"types" : ["node", "lodash", "express"]
}
}

只会引入 node 、 lodash 和 express 三个声明模块,其它的声明模块则不会被自动引入。 如果 types 被设置为 [] ,那么将不会自动引入任何声明模块。此时,如果想使用声明模块,只能在代码中手动引入了。 请记住,自动引入只对包含全局声明的模块有效。比如 jQuery ,我们不用手动 import 或者 /// 即可在任何文件中使用 $ 的类型。再比如,对于 import ‘foo’ ,编译器会分别在 node_modules 和 node_modules/@types 文件下查找 foo 模块和声明模块。 基于此,如果想让自定义声明的类型不需要手动引入就可以在任何地方使用,可以将其声明为全局声明 global ,然后让 files 或者 include 包含即可。 比如:

1
2
3
4
5
6
7
8
9
declare global {
const graphql: (query: TemplateStringsArray) => void;
namespace Gatsby {
interface ComponentProps {
children: () => React.ReactNode,
data: RootQueryType
}
}
}

这样的话,就可以在任何地方直接使用 graphql 和 Gatsby 对应的类型了。 配置复用 可以使用 extends 来实现配置复用,即一个配置文件可以继承另一个文件的配置属性。 比如,建立一个基础的配置文件 configs/base.json :

1
2
3
4
5
6
{
"compilerOptions": {
"noImplicitAny": true,
"strictNullChecks": true
}
}

然后,tsconfig.json 就可以引用这个文件的配置了:

1
2
3
4
5
6
7
{
"extends": "./configs/base",
"files": [
"main.ts",
"supplemental.ts"
]
}