# tsconfig.json 配置文件

如果一个目录下存在一个tsconfig.json文件,那么它意味着这个目录是TypeScript项目的根目录. tsconfig.json文件中指定了用来编译这个项目的根文件和编译选项。它主要包括这几个顶级配置。

{
    "compilerOptions": {
        "module": "commonjs",
        "noImplicitAny": true,
        "sourceMap": true
    },
    "files": [
        "core.ts",
    ],
    "include": [
        "src/**/*"
    ],
    "exclude": [
        "node_modules",
    ],
    "extends":"./tsconfig",
    "compileOnSave":true,
    "references":[]
}

# files

files可以配置一个数组列表,里面包含指定文件的相对路径和绝对路径。编译器在编译的时候,只会编译包含在files列表里面的文件。如果不指定,则取决于有没有include选项。如果没有include则默认编译根目录及所有子目录中的文件。files中列出的路径必须是指定文件,而不是某个文件夹,而且不能使用*,**/等通配符。

# include

include也可以指定要编译的路径列表,与files区别在于这里的路径可以是文件夹,也可以是文件;可以是相对路径也可以是绝对路径,可以使用通配符。比如:./src表示要编译src文件夹下的所有文件,以及子文件夹中包含的文件。

# exclude

exclude表示要排除的编译文件, 其使用和include相同。

# extends

extends可以指定一个其他的tsconfig.json文件路径,来继承这个配置文件里的配置。继承来的文件配置会覆盖当前的文件配置。

# compileOnSave

compileOnSave的值是true或者false。如果设置为true,在我们编译了项目中文件进行保存的时候,编译器会根据tsconfig.json重新生成文件。

# references

references是一个对象数组,用来指定要引入的项目。

# compilerOptions

compilerOptionstypescript编译最重要的配置。

  "compilerOptions": {
    // 基本的编译选项

    "target": "es5", // 用于指定编译完后的ECMAscript版本,是ES3,ES5,ES6。
    "module": "commonjs", // 用来指定模块标准
    "lib": ["es6","dom"], // 用于指定要包含在编译中的库文件,比如需要使用es6的一些新的写法就需要引入es6,需要操作dom就需要引入dom库。
    "allowJs": true,// 用来指定是否允许编译js文件,默认false
    "checkJs": true,// 用来指定检查和报告js中的错误。
    // "jsx": "preserve",                     /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
    "declaration": true,//用来指定是否在编译时生成相对应的.d.ts文件。如果设置我true,编译ts文件后会生成一个js文件和一个声明文件。
    "declarationMap": true,  // 生成.d.ts文件对应的sourceMap文件
    "sourceMap": true,// 用来指定编译时是否生成.map文件
    "outFile": "./",// 用来指定将输出文件指定为一个文件,它的值为一个文件路径名。比如./dist/main.js则输出的文件为main.js文件。但是只有设置module为amd和system时才支持这个配置。
    "outDir": "./", // 用来指定输出文件夹
    "rootDir": "./", // 编译时的根目录
    "composite": false, // 是否编译引用来的项目
    // "tsBuildInfoFile": "./",
    "removeComments": true,// 用来指定是否删除编译后文件中的注释
    // "noEmit": true,
    "importHelpers": true, //是否引入tslib里面的帮助函数
    // "downlevelIteration": true,
    "isolatedModules": true, // 指定是否将每个文件作为单独的模块

     // 严格类型检查配置项

    /* Strict Type-Checking Options */
    "strict": true, // 用于指定是否启动所有类型检查,如果这里设置为true,那么下面所有的类型检查相当于都设置为true了。
    "noImplicitAny": false,// 如果没有指定明确的类型,编译器会将其指定为any类型。如果设置为true,不设置明确类型会报错。
    "strictNullChecks": true, // null 和undefined检查。当设置为true的时候,null和undefined不能赋值给其他类型。
    "strictFunctionTypes": true,// 用来指定是否使用函数参数双向协变。
    "strictBindCallApply": true,  // 设置为true会对call,apply,bind参数进行严格检查。
    // "strictPropertyInitialization": true,  /* Enable strict checking of property initialization in classes. */
    "noImplicitThis": true, // 当this的值为any类型的时候会生成一个错误。
    "alwaysStrict": true,   // 以严格模式检查模块。

    // 额外的检测
    "noUnusedLocals": true,// 检查是否有定义了但是没有使用的变量
    "noUnusedParameters": true, // 用于检查函数参数是否定义了但是没有使用
    "noImplicitReturns": true,  // 用于检查函数是否有返回值
    "noFallthroughCasesInSwitch": true,  // 用于检查switch语句中每个case是否有break跳出语句

    // 模块的一些选项

    // "moduleResolution": "node",            /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
    // "baseUrl": "./",                       /* Base directory to resolve non-absolute module names. */
    // "paths": {},                           /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
    // "rootDirs": [],                        /* List of root folders whose combined content represents the structure of the project at runtime. */
    // "typeRoots": [],                       /* List of folders to include type definitions from. */
    // "types": [],                           /* Type declaration files to be included in compilation. */
    // "allowSyntheticDefaultImports": true,  /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
    "esModuleInterop": true,                  /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
    // "preserveSymlinks": true,              /* Do not resolve the real path of symlinks. */
    // "allowUmdGlobalAccess": true,          /* Allow accessing UMD globals from modules. */

    // sourcemap的相关配置

    // "sourceRoot": "",                      /* Specify the location where debugger should locate TypeScript files instead of source locations. */
    // "mapRoot": "",                         /* Specify the location where debugger should locate map files instead of generated locations. */
    // "inlineSourceMap": true,               /* Emit a single file with source maps instead of having a separate file. */
    // "inlineSources": true,                 /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */

    // 处于实验中的配置
    "experimentalDecorators": true, // 用于指定是否启用实验性质的装饰器特性
    "emitDecoratorMetadata": true,  // 用来指定是否为装饰器提供元数据支持
    /* Advanced Options */
    "forceConsistentCasingInFileNames": true  /* Disallow inconsistently-cased references to the same file. */
  }