在TS的项目中,TS最终都会被编译JS文件执行,TS编译器在编译TS文件的时候都会先在项目根目录的tsconfig.json
文件,根据该文件的配置进行编译,默认情况下,如果该文件没有任何配置,TS编译器会默认编译项目目录下所有的.ts、.tsx、.d.ts
文件。实际项目中,会根据自己的需求进行自定义的配置,下面就来详细了解下tsconfig.json
的文件配置。
文件选项配置
-
files
: 表示编译需要编译的单个文件列表1"files": [ 2 // 指定编译文件是src目录下的a.ts文件 3 "scr/a.ts" 4]
-
include
: 表示编译需要编译的文件或目录1"include": [ 2 // "scr" // 会编译src目录下的所有文件,包括子目录 3 // "scr/*" // 只会编译scr一级目录下的文件 4 "scr/*/*" // 只会编译scr二级目录下的文件 5]
-
exclude
:表示编译器需要排除的文件或文件夹默认排除
node_modules
文件夹下文件1"exclude": [ 2 // 排除src目录下的lib文件夹下的文件不会编译 3 "src/lib" 4]
-
extends
: 引入其他配置文件,继承配置1// 把基础配置抽离成tsconfig.base.json文件,然后引入 2"extends": "./tsconfig.base.json"
-
compileOnSave
:设置保存文件的时候自动编译vscode暂不支持该功能,可以使用’Atom’编辑器
1"compileOnSave": true
编译选项配置
-
compilerOptions
:配置编译选项编译选项配置非常繁杂,有很多配置,这里只列出常用的配置。
1"compilerOptions": { 2 "incremental": true, // TS编译器在第一次编译之后会生成一个存储编译信息的文件,第二次编译会在第一次的基础上进行增量编译,可以提高编译的速度 3 "tsBuildInfoFile": "./buildFile", // 增量编译文件的存储位置 4 "diagnostics": true, // 打印诊断信息 5 "target": "ES5", // 目标语言的版本 6 "module": "CommonJS", // 生成代码的模板标准 7 "outFile": "./app.js", // 将多个相互依赖的文件生成一个文件,可以用在AMD模块中,即开启时应设置"module": "AMD", 8 "lib": ["DOM", "ES2015", "ScriptHost", "ES2019.Array"], // TS需要引用的库,即声明文件,es5 默认引用dom、es5、scripthost,如需要使用es的高级版本特性,通常都需要配置,如es8的数组新特性需要引入"ES2019.Array", 9 "allowJS": true, // 允许编译器编译JS,JSX文件 10 "checkJs": true, // 允许在JS文件中报错,通常与allowJS一起使用 11 "outDir": "./dist", // 指定输出目录 12 "rootDir": "./", // 指定输出文件目录(用于输出),用于控制输出目录结构 13 "declaration": true, // 生成声明文件,开启后会自动生成声明文件 14 "declarationDir": "./file", // 指定生成声明文件存放目录 15 "emitDeclarationOnly": true, // 只生成声明文件,而不会生成js文件 16 "sourceMap": true, // 生成目标文件的sourceMap文件 17 "inlineSourceMap": true, // 生成目标文件的inline SourceMap,inline SourceMap会包含在生成的js文件中 18 "declarationMap": true, // 为声明文件生成sourceMap 19 "typeRoots": [], // 声明文件目录,默认时node_modules/@types 20 "types": [], // 加载的声明文件包 21 "removeComments":true, // 删除注释 22 "noEmit": true, // 不输出文件,即编译后不会生成任何js文件 23 "noEmitOnError": true, // 发送错误时不输出任何文件 24 "noEmitHelpers": true, // 不生成helper函数,减小体积,需要额外安装,常配合importHelpers一起使用 25 "importHelpers": true, // 通过tslib引入helper函数,文件必须是模块 26 "downlevelIteration": true, // 降级遍历器实现,如果目标源是es3/5,那么遍历器会有降级的实现 27 "strict": true, // 开启所有严格的类型检查 28 "alwaysStrict": true, // 在代码中注入'use strict' 29 "noImplicitAny": true, // 不允许隐式的any类型 30 "strictNullChecks": true, // 不允许把null、undefined赋值给其他类型的变量 31 "strictFunctionTypes": true, // 不允许函数参数双向协变 32 "strictPropertyInitialization": true, // 类的实例属性必须初始化 33 "strictBindCallApply": true, // 严格的bind/call/apply检查 34 "noImplicitThis": true, // 不允许this有隐式的any类型 35 "noUnusedLocals": true, // 检查只声明、未使用的局部变量(只提示不报错) 36 "noUnusedParameters": true, // 检查未使用的函数参数(只提示不报错) 37 "noFallthroughCasesInSwitch": true, // 防止switch语句贯穿(即如果没有break语句后面不会执行) 38 "noImplicitReturns": true, //每个分支都会有返回值 39 "esModuleInterop": true, // 允许export=导出,由import from 导入 40 "allowUmdGlobalAccess": true, // 允许在模块中全局变量的方式访问umd模块 41 "moduleResolution": "node", // 模块解析策略,ts默认用node的解析策略,即相对的方式导入 42 "baseUrl": "./", // 解析非相对模块的基地址,默认是当前目录 43 "paths": { // 路径映射,相对于baseUrl 44 // 如使用jq时不想使用默认版本,而需要手动指定版本,可进行如下配置 45 "jquery": ["node_modules/jquery/dist/jquery.min.js"] 46 }, 47 "rootDirs": ["src","out"], // 将多个目录放在一个虚拟目录下,用于运行时,即编译后引入文件的位置可能发生变化,这也设置可以虚拟src和out在同一个目录下,不用再去改变路径也不会报错 48 "listEmittedFiles": true, // 打印输出文件 49 "listFiles": true// 打印编译的文件(包括引用的声明文件) 50}
1{
2 "compilerOptions": {
3 /* Basic Options */
4 "target": "ES2017", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
5 "module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
6 // "lib": [], /* Specify library files to be included in the compilation. */
7 // "allowJs": true, /* Allow javascript files to be compiled. */
8 // "checkJs": true, /* Report errors in .js files. */
9 // "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
10 // "declaration": true, /* Generates corresponding '.d.ts' file. */
11 // "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
12 "sourceMap": true, /* Generates corresponding '.map' file. */
13 // "outFile": "./", /* Concatenate and emit output to single file. */
14 "outDir": "./dist", /* Redirect output structure to the directory. */
15 // "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
16 // "composite": true, /* Enable project compilation */
17 // "removeComments": true, /* Do not emit comments to output. */
18 // "noEmit": true, /* Do not emit outputs. */
19 // "importHelpers": true, /* Import emit helpers from 'tslib'. */
20 // "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
21 // "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
22 /* Strict Type-Checking Options */
23 "strict": true, /* Enable all strict type-checking options. */
24 // "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
25 // "strictNullChecks": true, /* Enable strict null checks. */
26 // "strictFunctionTypes": true, /* Enable strict checking of function types. */
27 // "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
28 // "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
29 // "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
30 /* Additional Checks */
31 // "noUnusedLocals": true, /* Report errors on unused locals. */
32 // "noUnusedParameters": true, /* Report errors on unused parameters. */
33 // "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
34 // "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
35 /* Module Resolution Options */
36 // "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
37 // "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
38 // "paths": {
39 // "src/*": ["src/*"],
40 // }, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
41 // "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
42 "typeRoots": ["./node_modules/@types", "./src/types"], /* List of folders to include type definitions from. */
43 // "types": [], /* Type declaration files to be included in compilation. */
44 // "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
45 "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
46 // "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
47 /* Source Map Options */
48 // "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
49 // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
50 // "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
51 // "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
52 /* Experimental Options */
53 // "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
54 // "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
55 },
56 "include": [
57 "src/**/*"
58 ],
59 "exclude": [
60 "src/**/*.test.ts"
61 ]
62}
工程引用配置
-
references
指定工程引用依赖在项目开发中,有时候我们为了方便将前端项目和后端
node
项目放在同一个目录下开发,两个项目依赖同一个配置文件和通用文件,但我们希望前后端项目进行灵活的分别打包,那么我们可以进行如下配置:1Project 2 - src 3 - client //客户端项目 4 - index.ts // 客户端项目文件 5 - tsconfig.json // 客户端配置文件 6 { 7 "extends": "../../tsconfig.json", // 继承基础配置 8 "compilerOptions": { 9 "outDir": "../../dist/client", // 指定输出目录 10 }, 11 "references": [ // 指定依赖的工程 12 {"path": "./common"} 13 ] 14 } 15 - common // 前后端通用依赖工程 16 - index.ts // 前后端通用文件 17 - tsconfig.json // 前后端通用代码配置文件 18 { 19 "extends": "../../tsconfig.json", // 继承基础配置 20 "compilerOptions": { 21 "outDir": "../../dist/client", // 指定输出目录 22 } 23 } 24 - server // 服务端项目 25 - index.ts // 服务端项目文件 26 - tsconfig.json // 服务端项目配置文件 27 { 28 "extends": "../../tsconfig.json", // 继承基础配置 29 "compilerOptions": { 30 "outDir": "../../dist/server", // 指定输出目录 31 }, 32 "references": [ // 指定依赖的工程 33 {"path": "./common"} 34 ] 35 } 36 - tsconfig.json // 前后端项目通用基础配置 37 { 38 "compilerOptions": { 39 "target": "es5", 40 "module": "commonjs", 41 "strict": true, 42 "composite": true, // 增量编译 43 "declaration": true 44 } 45 }
这样配置以后,就可以单独的构建前后端项目。
- 前端项目构建
1tsc -v src/client
- 后端项目构建
1tsc -b src/server
- 输出目录
1Project 2 - dist 3 - client 4 - index.js 5 - index.d.ts 6 - common 7 - index.js 8 - index.d.ts 9 - server 10 - index.js 11 - index.d.ts
个人笔记记录 2021 ~ 2025