三、配置文件

TypeScript

编译项目的时候,可以生成一个配置文件tsconfig.json

# yarn
yarn tsc --init
# npm
tsc --init
1
2
3
4

里面属性是typescript编译器配置的一些选项,下面是一些常用的配置及其含义,之后用到什么就进行补充。

# 配置文件注解

{
  "compilerOptions": {
    // 设置编译后的javascript采用的标准
    "target": "es5", 
    /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
    
    // 可以指定引用的标准库,默认是[],下面引用的是ES2015的标准库,避免Symbol和Promise的报错
    // 第二个DOM是DOM+BOM,使用console之类的用的,如果是空数组不需要写,默认就有,如果自己修改了这个数组,就要手动加上
    "lib": ["ES2015","DOM"],
    /* Specify library files to be included in the compilation. */
    
    // 输出的代码使用什么方式进行模块化,这里用的是commonJS,会把输入输出弄成require和module.export的方式
    "module": "commonjs",
    /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
    
    // 开启源代码映射,我们在调试的时候可以使用sourceMap文件去调试typescript源代码
    "sourceMap": true,
    /* Generates corresponding '.map' file. */
    
    // 设置编译结果输出的文件夹
    "outDir": "dist",
    /* Redirect output structure to the directory. */
    
    // 源代码ts文件所在的文件夹
    "rootDir": "src",
    /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
    
    /*类型检查相关 Strict Type-Checking Options */
    // 开启严格模式,对类型检查十分严格
    // 例如:any类型,也要严格写出来
    "strict": true,
    /* Enable all strict type-checking options. */
    
    // 检查变量不能为空null,可单独开启
    "strictNullChecks": true,              /* Enable strict null checks. */
  }
}
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

# 使用说明

有了配置文件之后,我们使用tsc命令编译整个项目的时候,配置文件才会生效,如果是单个文件,则不会起作用。

# yarn
yarn tsc
# npm
tsc
1
2
3
4
更新时间: 2021-09-15 12:03