tsconfig.json有以下顶层属性:

  • compileOnSave
  • compilerOptions
  • exclude
  • extends
  • files
  • include
  • references
  • typeAcquisition

    生成tsconfig.json

    通过 tsc —init 初始化 tsconfig.json 文件。

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

    ```json { “compilerOptions”: {

    / 基本选项 / “target”: “es5”, // 指定 ECMAScript 目标版本: ‘ES3’ (default), ‘ES5’, ‘ES6’/‘ES2015’, ‘ES2016’, ‘ES2017’, or ‘ESNEXT’ “module”: “commonjs”, // 指定使用模块: ‘commonjs’, ‘amd’, ‘system’, ‘umd’ or ‘es2015’ “lib”: [], // 指定要包含在编译中的库文件 “allowJs”: true, // 允许编译 javascript 文件 “checkJs”: true, // 报告 javascript 文件中的错误 “jsx”: “preserve”, // 指定 jsx 代码的生成: ‘preserve’, ‘react-native’, or ‘react’ “declaration”: true, // 生成相应的 ‘.d.ts’ 文件 “sourceMap”: true, // 生成相应的 ‘.map’ 文件 “outFile”: “./“, // 将输出文件合并为一个文件 “outDir”: “./“, // 指定输出目录 “rootDir”: “./“, // 用来控制输出目录结构 —outDir. “removeComments”: true, // 删除编译后的所有的注释 “noEmit”: true, // 不生成输出文件 “importHelpers”: true, // 从 tslib 导入辅助工具函数 “isolatedModules”: true, // 将每个文件作为单独的模块 (与 ‘ts.transpileModule’ 类似).

    / 严格的类型检查选项 / “strict”: true, // 启用所有严格类型检查选项 “noImplicitAny”: true, // 在表达式和声明上有隐含的 any类型时报错 “strictNullChecks”: true, // 启用严格的 null 检查 “noImplicitThis”: true, // 当 this 表达式值为 any 类型的时候,生成一个错误 “alwaysStrict”: true, // 以严格模式检查每个模块,并在每个文件里加入 ‘use strict’

    / 额外的检查 / “noUnusedLocals”: true, // 有未使用的变量时,抛出错误 “noUnusedParameters”: true, // 有未使用的参数时,抛出错误 “noImplicitReturns”: true, // 并不是所有函数里的代码都有返回值时,抛出错误 “noFallthroughCasesInSwitch”: true, // 报告 switch 语句的 fallthrough 错误。(即,不允许 switch 的 case 语句贯穿)

    / 模块解析选项 / “moduleResolution”: “node”, // 选择模块解析策略: ‘node’ (Node.js) or ‘classic’ (TypeScript pre-1.6) “baseUrl”: “./“, // 用于解析非相对模块名称的基目录 “paths”: {}, // 模块名到基于 baseUrl 的路径映射的列表 “rootDirs”: [], // 根文件夹列表,其组合内容表示项目运行时的结构内容 “typeRoots”: [], // 包含类型声明的文件列表 “types”: [], // 需要包含的类型声明文件名列表 “allowSyntheticDefaultImports”: true, // 允许从没有设置默认导出的模块中默认导入。

    / Source Map Options / “sourceRoot”: “./“, // 指定调试器应该找到 TypeScript 文件而不是源文件的位置 “mapRoot”: “./“, // 指定调试器应该找到映射文件而不是生成文件的位置 “inlineSourceMap”: true, // 生成单个 soucemaps 文件,而不是将 sourcemaps 生成不同的文件 “inlineSources”: true, // 将代码与 sourcemaps 生成到一个文件中,要求同时设置了 —inlineSourceMap 或 —sourceMap 属性

    / 其他选项 / “experimentalDecorators”: true, // 启用装饰器 “emitDecoratorMetadata”: true // 为装饰器提供元数据的支持 } }

  1. ```json
  2. {
  3. "compilerOptions": {
  4. /* Visit https://aka.ms/tsconfig.json to read more about this file */
  5. /* Projects */
  6. // "incremental": true, /* TS编译器在第一次编译之后会生成一个存储编译信息的文件,第二次编译会在第一次的基础上进行增量编译,可以提高编译的速度 */
  7. // "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
  8. // "tsBuildInfoFile": "./", /* 增量编译文件的存储位置 */
  9. // "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects */
  10. // "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
  11. // "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
  12. /* Language and Environment */
  13. "target": "esnext", /* 编译后的JavaScript版本 */
  14. // "lib": [], /* TS需要引用的库,即声明文件,es5 默认引用dom、es5、scripthost,如需要使用es的高级版本特性,通常都需要配置,如es8的数组新特性需要引入"ES2019.Array" */
  15. "jsx": "preserve", /* Specify what JSX code is generated. */
  16. "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */
  17. "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
  18. // "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h' */
  19. // "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
  20. // "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using `jsx: react-jsx*`.` */
  21. // "reactNamespace": "", /* Specify the object invoked for `createElement`. This only applies when targeting `react` JSX emit. */
  22. // "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
  23. // "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
  24. /* Modules */
  25. "module": "esnext", /* module 指定要使用的模块化的规范 es6 ES2015 commonjs esnext */
  26. // "rootDir": "./", /* Specify the root folder within your source files. */
  27. "moduleResolution": "node", /* 模块解析策略,ts默认用node的解析策略,即相对的方式导入 */
  28. // "baseUrl": "./", /* 解析非相对模块的基地址,默认是当前目录 */
  29. // "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
  30. // "rootDirs": ["src","out"], /* 指定输出文件目录(用于输出),用于控制输出目录结构 将多个目录放在一个虚拟目录下,用于运行时,即编译后引入文件的位置可能发生变化,这也设置可以虚拟src和out在同一个目录下,不用再去改变路径也不会报错 */
  31. "typeRoots": ["src/types/**/*"], /* 声明文件目录,默认时node_modules/@types*/
  32. "types": ["node"], /* 加载的声明文件包 node react */
  33. // "allowUmdGlobalAccess": true, /* 允许在模块中全局变量的方式访问umd模块 */
  34. // "resolveJsonModule": true, /* Enable importing .json files */
  35. // "noResolve": true, /* Disallow `import`s, `require`s or `<reference>`s from expanding the number of files TypeScript should add to a project. */
  36. /* JavaScript Support */
  37. // "allowJs": true, /* 允许编译器编译JS,JSX文件 */
  38. // "checkJs": true, /* 允许在JS文件中报错,通常与allowJS一起使用 */
  39. // "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from `node_modules`. Only applicable with `allowJs`. */
  40. /* Emit */
  41. // "declaration": true, /* 生成声明文件,开启后会自动生成声明文件 */
  42. // "declarationMap": true, /* 设置生成*.d.ts.map文件(sourceMap) */
  43. // "emitDeclarationOnly": true, /* 不生成js文件,仅仅生成*.d.ts和*.d.ts.map */
  44. // "sourceMap": true, /* 是否映射文件 生成目标文件的sourceMap文件 */
  45. // "outFile": "./", /* 将多个相互依赖的文件生成一个文件,可以用在AMD模块中,即开启时应设置"module": "AMD" ES6报错 */
  46. // "outDir": "./", /* 编译之后输出的位置 */
  47. // "removeComments": true, /* 移除注释(块注释 行注释) */
  48. // "noEmit": true, /* 不输出文件,即编译后不会生成任何js文件 */
  49. // "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
  50. // "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types */
  51. // "downlevelIteration": true, /* 降级遍历器实现,如果目标源是es3/5,那么遍历器会有降级的实现 */
  52. // "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
  53. // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
  54. // "inlineSourceMap": true, /* 生成目标文件的inline SourceMap,inline SourceMap会包含在生成的js文件中 */
  55. // "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */
  56. // "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */
  57. // "newLine": "crlf", /* Set the newline character for emitting files. */
  58. // "stripInternal": true, /* Disable emitting declarations that have `@internal` in their JSDoc comments. */
  59. // "noEmitHelpers": true, /* 通过tslib引入helper函数,文件必须是模块 */
  60. // "noEmitOnError": true, /* 有错误时 不会生成编译后的文件 */
  61. // "preserveConstEnums": true, /* 希望使用 const enums 保留这种映射 保留 const 和 enum 声明 */
  62. // "declarationDir": "./", /* 指定生成声明文件(.d.ts)存放目录 */
  63. // "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */
  64. /* Interop Constraints */
  65. // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
  66. // "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
  67. "esModuleInterop": true, /* 允许export=导出,由import from 导入 */
  68. // "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
  69. "forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
  70. /* Type Checking */
  71. "strict": true, /* Enable all strict type-checking options. */
  72. "noImplicitAny": true, /* any必须为显式声明 隐式声明会报错 不允许隐式的any类型 */
  73. // "strictNullChecks": true, /* 不允许把null、undefined赋值给其他类型的变量 */
  74. // "strictFunctionTypes": true, /* 不允许函数参数双向协变 */
  75. // "strictBindCallApply": true, /* 严格的bind/call/apply检查. */
  76. // "strictPropertyInitialization": true, /* 类的实例属性必须初始化 */
  77. // "noImplicitThis": true, /* 不允许this有隐式的any类型 */
  78. // "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */
  79. // "alwaysStrict": true, /* 在代码中注入'use strict' */
  80. // "noUnusedLocals": true, /* 检查只声明、未使用的局部变量(只提示不报错) */
  81. // "noUnusedParameters": true, /* 检查未使用的函数参数(只提示不报错)*/
  82. // "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
  83. // "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
  84. // "noFallthroughCasesInSwitch": true, /* 防止switch语句贯穿(即如果没有break语句后面不会执行) */
  85. // "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */
  86. // "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
  87. // "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type */
  88. // "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
  89. // "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */
  90. /* Completeness */
  91. // "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
  92. "skipLibCheck": true /* Skip type checking all .d.ts files. */
  93. },
  94. "include": ["src/js/**/*"], /* 指定哪些ts文件会被编译*/
  95. "exclude": ["node_modules"], /* 不需要编辑的文件列表*/
  96. "files": ["./src/index.ts"] /* 指定被编译的文件列表 只有需要编译的文件少时才会用到*/
  97. }