【TypeScript 】tsconfig.json compilerOptions 配置项 详解

772 阅读3分钟

前言

在typescript 项目中,tsconfig.json 文件指定编译项目所需的根文件和编译器选项。

正文

在使用tsc --init命令后,项目会生成下面的文件tsconfig.json。下面对常用的选项进行说明。

{
  "compilerOptions": {
    /* Visit https://aka.ms/tsconfig to read more about this file */

    /* 语言和环境 */
    "target": "es2016" /* 指定 ECMAScript 目标版本: es3、es5、es6/es2015、es2016、es2017、es2018、es2019、es2020、es2021、es2022、esnext */,
    // "lib": [],                                  /* 指定要包含在编译中的库文件,比如:["ESNext", "DOM"] */
    // "jsx": "preserve",                          /* 指定 jsx 代码的生成: 'preserve'(jsx不转)、'react'(React.createElement)、 'react-native' */
    // "experimentalDecorators": true,             /* 启用装饰器 */
    // "emitDecoratorMetadata": true,              /* 为装饰器提供元数据的支持 */
    // "noLib": true,                              /* 不引入任何库文件, 包括默认的 lib.d.ts. */
    // "useDefineForClassFields": true,            /* 将 class 声明中的字段语义从 [[Set]] 变更到 [[Define]] */

    /* Modules */
    "module": "commonjs",                          /* 指定使用模块: commonjs、amd、umd、system、es6/es2015、es2020、es2022、esnext、node16、nodenext */
    // "rootDir": "./",                            /* 指定输入文件的根目录 */
    // "moduleResolution": "node",                 /* 模块解析的方式. 'node', 或者 'classic' (默认). */
    // "baseUrl": "./",                            /* 于解析非相对模块名称的根目录 */
    // "paths": {},                                /* 模块名到基于 baseUrl 的路径映射的列表 */
    // "rootDirs": [],                             /* 根文件夹列表,其组合内容表示项目运行时的结构内容 */
    // "typeRoots": [],                            /* 包含类型声明的文件列表,如: './node_modules/@types' */
    // "types": [],                                /* 需要包含的类型声明文件名列表 */
    // "resolveJsonModule": true,                  /* 解析JSON模块 */
    // "noResolve": true,                          /* 在被编译的文件列表中不列出由三斜线引用的和导入的目标文件 */

    /* JavaScript Support */
    // "allowJs": true,                            /* 允许编译 JavaScript 文件 */
    // "checkJs": true,                            /* 报告 javascript 文件中的错误 */

    /* Emit */
    // "declaration": true,                        /* 生成对应的 '.d.ts' 文件.. */
    // "emitDeclarationOnly": true,                /* 仅输出 d.ts 文件,不处理 JavaScript 文件 */
    // "sourceMap": true,                          /* 生成对应的 '.map' 文件. */
    // "outFile": "./",                            /* 将输出文件合并为一个文件 */
    // "outDir": "./",                             /* 指定输出目录 */
    // "removeComments": true,                     /* 删除编译后的所有的注释 */
    // "noEmit": true,                             /* 不进行文件输出. */
    // "importHelpers": true,                      /* 从 tslib 导入辅助工具函数 */
    // "sourceRoot": "",                           /* 指定调试器应该找到 TypeScript 文件而不是源文件的位置 */
    // "mapRoot": "",                              /* 指定调试器应该找到映射文件而不是生成文件的位置 */
    // "inlineSourceMap": true,                    /* 生成单个 soucemaps 文件,而不是将 sourcemaps 生成不同的文件*/
    // "inlineSources": true,                      /* 将代码与 sourcemaps 生成到一个文件中,要求同时设置了 --inlineSourceMap 或 --sourceMap 属性 */
    // "emitBOM": true,                            /* 在输出文件的开头添加 UTF-8 字节顺序标记 (BOM). */
    // "newLine": "crlf",                          /* 输出文件时使用指定的换行符 crlf lf/
    // "stripInternal": true,                      /* 不输出有 /** @internal */ JSDoc 标注代码的声明 */
    // "noEmitHelpers": true,                      /* 不在编译结果中生成自定义的像 __extends 的辅助函数 */
    // "noEmitOnError": true,                      /* 在有任何错误时都不输出文件 */
    // "preserveConstEnums": true,                 /* 在生成的代码中保留常量枚举声明  */

    /* Interop Constraints */
    // "isolatedModules": true,                    /* 确保每个文件都可以安全传输,而不依赖于其他导入 */
    // "allowSyntheticDefaultImports": true,       /* Allow 'import x from y' when a module doesn't have a default export. */
    "esModuleInterop": true,                       /* 提供两个helper函数__importStar和__importDefault分别处理import * as和import default。 */
    "forceConsistentCasingInFileNames": true,      /* 禁止对同一个文件大小写不一致的引用 */

    /* Type Checking */
    "strict": true,                                 /* 严格模式 */
    // "noImplicitAny": true,                       /* 在表达式或者声明包含隐式的 'any' 类型是报错 */
    // "strictNullChecks": true,                    /* 启用严格的 null 检查 */
    // "strictPropertyInitialization": true,        /* 设置为 true 时,当在构造函数中声明了类属性但未设置时,TypeScript 将引发错误。 */
    // "noImplicitThis": true,                      /* 当 this 表达式值为 any 类型的时候,生成一个错误 */
    // "alwaysStrict": true,                        /* 以严格模式检查每个模块,并在每个文件里加入 'use strict' */
    // "noUnusedLocals": true,                      /* 有未使用的变量时,抛出错误 */
    // "noUnusedParameters": true,                  /* 有未使用的参数时,抛出错误 */
    // "noImplicitReturns": true,                   /* 当函数中有代码路径没有返回值时报错 */
    // "noFallthroughCasesInSwitch": true,          /* 对 switch 语句中贯穿的 case 报错 */
    // "allowUnreachableCode": true,                /* 不对无法到达的语句报错 */

    /* Completeness */
    // "skipDefaultLibCheck": true,                 /* 跳过默认库文件检查 */
    "skipLibCheck": true                            /* 跳过所有 .d.ts 文件的类型检查 */
  }
}