以前咱们讲了 TypeScript(一) —— 了解并快速入门 ,如今展开说明一下配置文件里面选项的含义。
编译项目的时候,能够生成一个配置文件tsconfig.json
javascript
# yarn yarn tsc --init # npm tsc --init
里面属性是typescript
编译器配置的一些选项,下面是一些经常使用的配置及其含义,以后用到什么就进行补充。java
{ "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. */ } }
有了配置文件以后,咱们使用tsc
命令编译整个项目的时候,配置文件才会生效,若是是单个文件,则不会起做用。typescript
# yarn yarn tsc # npm tsc