以下是一些常用的tsconfig的配置选项,更多的ts配置项请参考tsconfig配置
创建 tsconfig.base.json 文件
用来添加ts的基本配置
- 需要编译的单个文件列表
"files": [
"src/a.ts"
]
- 需要编译的文件或目录,支持 * 通配符
"include": [
"src" /*src下的所有文件*/
],
- 排除不需要编译的文件或目录
"exclude": [
"src/lib" /*排除src/lib下的文件,默认会排除node_modules和声明文件*/
]
tsconfig.base.json 的配置文件
{
"files": [ /*需要编译的单个文件的列表*/
"src/a.ts"
],
"include": [ /*需要编译的文件或目录, 支持 * 通配符*/
"src" /*src下的所有文件*/
],
"exclude": [ /*排除需要编译的文件或目录*/
"src/lib" /*排除src/lib下的文件,默认会排除node_modules和声明文件*/
]
}
tsconfig.json 文件
- 导入基本配置
"extends": "./tsconfig.base",
- 保存自动编译 vscode不支持
"compileOnSave": true,
- 工程之间的依赖关系
"references": [{"path": ""}]
以下是compilerOptions 编译选项配置项
- 增量编译,提高二次编译的速度
"incremental": true,
- 增量编译的存储位置
"tsBuildInfoFile": "./",
- 打印诊断信息
"diagnostics": true,
- 目标语言的版本: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'.
"target": "es5",
- 生成代码的模块标准: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'.
"module": "commonjs",
- TS 需要引入的库,即声明文件,es5 默认引入"dom" "es5" "scripthost"
"lib": [],
- 允许编译js文件 (js\jsx)
"allowJs": true,
- 允许在JS文件中报错,通常与allowJs一起使用
"checkJs": true,
- 生成特定的jsx代码: 'preserve', 'react-native', or 'react'.
"jsx": "preserve",
- 生成声明文件 '.d.ts'
"declaration": true,
- 声明文件的路径
"declarationDir": "./",
- 只生成声明文件
"emitDeclarationOnly": true,
- 为声明文件生成sourceMap
"declarationMap": true,
- 声明目标文件的 sourceMap
"sourceMap": true,
- 生成目标文件的 inline sourceMap,包含在生成的js文件中
"inlineSourceMap": true,
- 将多个相互依赖的文件生成一个文件,可以用在AMD模块中
outFile": "./",
- 指定输出目录
"outDir": "./",
- 指定输入文件目录 (用于控制输出目录结构)
"rootDir": "./",
- 工程可以被引用,并且可以进行增量编译
"composite": true,
- 删除注释
"removeComments": true,
- 不生成任何文件
"noEmit": true,
- 发生错误时是否输出文件
"noEmitOnError": false,
- 不生成 helper 函数,需要额外安装 ts-helpers, 在类的继承中编译成js文件会生成helper函数,增大了文件的体积,所以会选择不生成helper函数,但必须在tslib中引入helper函数
"noEmitHelpers": true,
- 通过 tslib 引入 helper 函数,文件必须是模块
"importHelpers": true,
- 降级遍历器的实现 'ES5' or 'ES3'.
"downlevelIteration": true,
- 将每个文件作为单独的模块进行传输(类似于'传输模块').
"isolatedModules": true,
- 开启所有的严格检查
"strict": true,
- 允许隐式的any类型
"noImplicitAny": true,
- 允许把null undefined 赋值给其他类型变量
"strictNullChecks": true,
- 允许函数参数双向协变
"strictFunctionTypes": true,
- 严格的bind call apply 检查
"strictBindCallApply": true,
- 类的实例属性必须初始化
"strictPropertyInitialization": true,
- 不允许this有隐式的any
"noImplicitThis": true,
- 在代码中注入 "use strict"
"alwaysStrict": true,
- 检查只声明,未使用的局部变量
"noUnusedLocals": true,
- 检查未使用的函数参数
"noUnusedParameters": true,
- 每个分支都要有返回值
"noImplicitReturns": true,
- 防止switch语句贯穿
"noFallthroughCasesInSwitch": true,
- 在索引签名结果中包含“undefined”
"noUncheckedIndexedAccess": true,
- 模块解析策略: 'node' (Node.js) or 'classic' (TypeScript pre-1.6).
"moduleResolution": "node",
- 解析非相对模块的基地址
"baseUrl": "./",
- 路径映射,相对于 'baseUrl'
"paths": {},
- 将多个目录放在一个虚拟目录下用于运行时
"rootDirs": [],
- 声明文件目录 默认是node_modules/@type
"typeRoots": [],
- 声明文件包
"types": [],
- 允许从没有默认导出的模块中进行默认导入。这不会影响代码的编译,只会影响类型检查。
"allowSyntheticDefaultImports": true,
- 允许export = 导出,有import from 导入
"esModuleInterop": true,
- 允许使用umd全局变量
"allowUmdGlobalAccess": true,
- 打印输出的文件
"listEmittedFiles": true,
- 打印编译文件(包括引用的声明文件)
"listFiles": true,
- 跳过声明文件的类型检查。
"skipLibCheck": true,
- 不允许对同一文件进行大小写不一致的引用。
"forceConsistentCasingInFileNames": true
tsconfig.json 文件
{
"compilerOptions": {
/* Visit https://aka.ms/tsconfig.json to read more about this file */
/* Basic Options */
// "incremental": true, /* 增量编译,提高二次编译的速度 */
// "tsBuildInfoFile": "./", /* 增量编译的存储位置 */
// "diagnostics": true, /* 打印诊断信息 */
"target": "es5", /* 目标语言的版本: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
"module": "commonjs", /* 生成代码的模块标准: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
// "lib": [], /* TS 需要引入的库,即声明文件,es5 默认引入"dom" "es5" "scripthost" */
// "allowJs": true, /* 允许编译js文件 (js\jsx) */
// "checkJs": true, /* 允许在JS文件中报错,通常与allowJs一起使用 */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
// "declaration": true, /* 生成声明文件 '.d.ts' */
// "declarationDir": "./", /* 声明文件的路径 */
// "emitDeclarationOnly": true, /* 只生成声明文件 */
// "declarationMap": true, /* 为声明文件生成sourceMap */
// "sourceMap": true, /* 声明目标文件的 sourceMap */
// "inlineSourceMap": true, /* 生成目标文件的 inline sourceMap,包含在生成的js文件中 */
// "outFile": "./", /* 将多个相互依赖的文件生成一个文件,可以用在AMD模块中 */
// "outDir": "./", /* 指定输出目录 */
// "rootDir": "./", /* 指定输入文件目录 (用于控制输出目录结构) */
// "composite": true, /* 工程可以被引用,并且可以进行增量编译 */
// "removeComments": true, /* 删除注释 */
// "noEmit": true, /* 不生成任何文件 */
// "noEmitOnError": false, /* 发生错误时是否输出文件 */
// "noEmitHelpers": true, /* 不生成 helper 函数,需要额外安装 ts-helpers, 在类的继承中编译成js文件会生成helper函数,增大了文件的体积,所以会选择不生成helper函数,但必须在tslib中引入helper函数 */
// "importHelpers": true, /* 通过 tslib 引入 helper 函数,文件必须是模块 */
// "downlevelIteration": true, /* 降级遍历器的实现 'ES5' or 'ES3'. */
// "isolatedModules": true, /* 将每个文件作为单独的模块进行传输(类似于'传输模块'). */
/* Strict Type-Checking Options */
"strict": true, /* 开启所有的严格检查 */
// "noImplicitAny": true, /* 允许隐式的any类型 */
// "strictNullChecks": true, /* 允许把null undefined 赋值给其他类型变量 */
// "strictFunctionTypes": true, /* 允许函数参数双向协变 */
// "strictBindCallApply": true, /* 严格的bind call apply 检查 */
// "strictPropertyInitialization": true, /* 类的实例属性必须初始化 */
// "noImplicitThis": true, /* 不允许this有隐式的any */
// "alwaysStrict": true, /* 在代码中注入 "use strict" */
/* Additional Checks */
// "noUnusedLocals": true, /* 检查只声明,未使用的局部变量 */
// "noUnusedParameters": true, /* 检查未使用的函数参数 */
// "noImplicitReturns": true, /* 每个分支都要有返回值 */
// "noFallthroughCasesInSwitch": true, /* 防止switch语句贯穿 */
// "noUncheckedIndexedAccess": true, /* 在索引签名结果中包含“undefined” */
/* Module Resolution Options */
// "moduleResolution": "node", /* 模块解析策略: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
// "baseUrl": "./", /* 解析非相对模块的基地址 */
// "paths": {}, /* 路径映射,相对于 'baseUrl' */
// "rootDirs": [], /* 将多个目录放在一个虚拟目录下用于运行时 */
// "typeRoots": [], /* 声明文件目录 默认是node_modules/@types */
// "types": [], /* 声明文件包 */
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true, /* 允许export = 导出,有import from 导入*/
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
// "allowUmdGlobalAccess": true, /* 允许使用umd全局变量 */
// "listEmittedFiles": true, /* 打印输出的文件 */
// "listFiles": true, /* 打印编译文件(包括引用的声明文件) */
/* Source Map Options */
// "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. */
/* Experimental Options */
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
/* Advanced Options */
"skipLibCheck": true, /* 跳过声明文件的类型检查。*/
"forceConsistentCasingInFileNames": true /* 不允许对同一文件进行大小写不一致的引用。 */
},
"extends": "./tsconfig.base", /*导入基本配置*/
"compileOnSave": true, /*保存自动编译 vscode不支持*/
// "references": [{"path": ""}] /* 工程之间的依赖关系 */
}