【TS】tsconfig.json 全配置项解析

文章目录

  • tsconfig.json 是ts编译器的配置文件,ts编译器可以根据它的信息来对代码进行编译
/*
	"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"
      		],
    	},
	}
}
  • 实际运用:
{
  "compileOnSave": false,
  "buildOnSave": false,
  "compilerOptions": {
    "baseUrl": ".",
    "outDir": "build",
    "module": "esnext",
    "target": "es6",
    "jsx": "react-jsx",
    "moduleResolution": "node",
    "allowSyntheticDefaultImports": true,
    "lib": [
      "es6",
      "dom"
    ],
    "sourceMap": true,
    "allowJs": true,
    "rootDir": "./",
    "forceConsistentCasingInFileNames": true,
    "noImplicitReturns": true,
    "noImplicitThis": true,
    "noImplicitAny": false,
    "importHelpers": true,
    "strictNullChecks": true,
    "suppressImplicitAnyIndexErrors": true,
    "noUnusedLocals": true,
    "skipLibCheck": true,
    "paths": {
      "@/*": [
        "./src/*"
      ],
      "ice": [
        ".ice"
      ]
    },
    "resolveJsonModule": true
  },
  "include": [
    "src",
    ".ice",
    "src/typings.d.ts"
  ],
  "exclude": [
    "node_modules",
    "build",
    "public"
  ]
}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一颗不甘坠落的流星

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值