个人博客 个人博客
首页
  • 前端
  • 后端
  • Git
  • Docker
  • 网络
  • 操作系统
工具
阅读
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

董先亮

前端react开发
首页
  • 前端
  • 后端
  • Git
  • Docker
  • 网络
  • 操作系统
工具
阅读
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • HTML

  • CSS

  • JavaScript

  • TypeScript

    • interface 与 type
    • 类型
    • 工具类
    • tsconfig详解
    • 前端基础
    • TypeScript
    NeverStop1024
    2023-02-02
    目录

    tsconfig详解

    # 完整配置

    /*
    	"include" 用来指定哪些 ts 文件需要被编译
    		路径:** 表示任意目录,*表示任意文件
    		默认值:["src"]
    	"exclude" 不需要被编译的文件目录
    		默认值:["node_modules", "bower_components", jspm_packages]
    	"files" 指定被编译的文件,只有文件少时才会用到
    	"complierOptions" 编译器选项
    */
    {
    	"include": [
    		"./src/**/*"
    	],
    	"exclude": [
    		"./src/hello/**/*"
    	],
    	"files": [
    		"roee.ts",
    		"asfe.ts"
    	]
    	"compilerOptions": {
    		// 用来设置编译后的文件是否使用严格模式
    		"alwaysStrict": true,
    		
    		// allowJs 是否对js文件进行编译,默认false
    		"allowJs": true,
    		
    		// 允许通过 import x from ‘y’ 即使模块没有显式指定 default 导出
    		"allowSyntheticDefalutImports": true,
    
    		// 不报告执行不到的代码错误。
    		"allowUnreachableCode" : true,
    		
    		// 不报告未使用的标签错误
    		"allowUnusedLabels": false, 
    		
    		// 允许在模块中全局变量的方式访问umd模块
    		"allowUmdGlobalAccess": true, 
    		
    	 	// 工作根目录
    		"baseUrl": ".",
    		
    		// checkJs 是否检查js代码是否符合语法规范,默认false
    		"checkJs": true,
    		
    		// 是否自动创建类型声明文件
    		"declaration": true, 
    		
    		// 类型声明文件的输出目录
        	"declarationDir": "./lib", 
        	
        	// 为声明文件生成sourceMap
        	"declarationMap": true, 
    		
    		// es 模块 互操作,屏蔽 ESModule 和 CommonJS 之间的差异
    		"esModuleInterop": true,
    		
    		// 只生成声明文件,而不会生成js文件
    		"emitDeclarationOnly": true, 
    		
    		// 对文件名称强制区分大小写
    		"forceConsistentCasingInFileNames": true,
    		
    		// 是否将没有 import/export 的文件视为(全局而非模块化)脚本文件.
    		"isolatedModules": true,
    		
    		// 生成目标文件的inline SourceMap,inline SourceMap会包含在生成的js文件中
    		"inlineSourceMap": true, 
    		
    		// 指定将 JSX 编译成什么形式
    		"jsx": "react-jsx",
    		
    		// lib 指定项目中要使用的库
    		"lib": [
    			"dom",
          		"dom.iterable",
          		"ESNext"
    		],
    		
    		// 打印输出文件
    		"listEmittedFiles": true, 
    		
    		// 打印编译的文件(包括引用的声明文件)
        "listFiles": true
        
    		// module 指定要使用的模块化的规范
    		// none, commonjs, amd, system, umd, es6, es2015, es2020, esnext
    		"module": "esnext",
    		
    		// 模块解析策略
    		"moduleResolution": "node",
    		
    		// 为 swith 语句启用错误报告
    		"noFallthroughCasesInSwitch": true,
    		
    		// noEmitOnError 当有错误时不生成编译后产生的文件
    		"noEmitOnError": true,
    		
    		// noEmit 不生成编译后产生的文件(只进行类型检查)
    		"noEmit": true,
    		
    		// 不允许隐式的 any 类型
    		"noImplicitAny": true,
    		
    		// 不允许不明确类型的 this
    		"noImplicitThis": true,
    		
    		// 检查只声明、未使用的局部变量(只提示不报错)
    		"noUnusedLocals": true, 
    		
    		// 检查未使用的函数参数(只提示不报错)
        "noUnusedParameters": true, 
        
        // 每个分支都会有返回值
        "noImplicitReturns": true, 
        
    		// 允许导入扩展名为 .json的模块
    		"resolveJsonModule": true,
    		
    		// removeComments 是否移除注释
    		"removeComments": true,
    		
    		// 跳过声明文件的类型检查
    		"skipLibCheck": true,
    		
    		// 所有严格模式的总开关
    		"strict": true, 
    		
    		// 严格的检查空值
    		"strictNullChecks": true,
    		
    		// 是否生成map文件
    		"sourceMap": true, 
    		
    		// 不允许函数参数双向协变
    		"strictFunctionTypes": true, 
    		
    		// 类的实例属性必须初始化
         "strictPropertyInitialization": true, 
         
         // 严格的bind/call/apply检查
         "strictBindCallApply": true, 
    		
    		// 指定引入的类型声明文件,默认是自动引入所有声明文件,一旦指定该选项,则会禁用自动引入,改为只引入指定的类型声明文件,如果指定空数组[]则不引用任何文件
    		"types": [ 
          		"node", // 引入 node 的类型声明
        	],
        	
        	// 声明文件目录,默认时node_modules/@types
        	"typeRoots": [], 
        	
    		// target 用来指定ts被编译为的es版本
    		// es3, es5, es6, es2015, es2016, es2017, es2018, es2019, es2020,
    		"target": "es6",
    		
    		// outDir 用来指定编译后文件所在的目录
    		"outDir": "./dist",
    		
    		// outFile 将代码合并为一个文件
    		"outFile": "./dist.app.js",
    		
    		// 指定模块的路径,和baseUrl有关联,和webpack中resolve.alias配置一样
    		"paths": { 
          		"src": [ // 指定后可以在文件之直接 import * from 'src';
           		 "./src"
          		],
        	},
    	}
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169

    # 问题

    1. include与files同时配置是否冲突
    {
      "compilerOptions": {
        // ...
      },
      "include": [
        "src/**/*"
      ],
      "files": [
        "src/index.ts"
      ]
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11

    同时配置不冲突,在这种情况下,只有src/index.ts文件和src目录中的所有文件都会被编译。

    编辑 (opens new window)
    上次更新: 2023/02/02
    工具类

    ← 工具类

    最近更新
    01
    mock使用
    07-12
    02
    websocket即时通讯
    07-12
    03
    前端面试题
    07-09
    更多文章>
    Theme by Vdoing | Copyright © 2022-2023 NeverStop1024 | MIT License
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式