TypeScript配置文件设置详解


TypeScript配置文件名时tsconfig.json,文件内部写json,其中比较常用的配置属性有一下几种

1. include

用来指定那些目录下的配置文件需要被编译

//       ** 表示任意目录
//       *  表示任意文件
{
	"include" : [
		"./scr/**/*"
	]
}

2. exclude

用来指定那些目录的文件不需要被编译

{
	"exclude":[
		"./static/**/*"
	]
}

3. extends

指定继承哪个配置文件的配置

{
	"extends": ""
}

4. files

指定被编译的文件

{
	"files":[
		"./src/index.ts"
	]
}

5. compilerOptions

编译器选项

5.1 target

指定ts编译后的js版本,默认ES3

{
	"compilerOptions":{
		"target":"ES6"
	}
}

5.2 module

指定编译后的模块类型,默认commonjs

{
	"compilerOptions":{
		"target":"ES6",
		"module": "es2015"
	}
}

5.3 lib

编译时需要引入的库文件

{
	"compilerOptions":{
		"target":"ES6",
		"module": "es2015",
		"lib": ["amd"]
	}
}

5.4 outDir

指定编译文件的输出位置

{
	"compilerOptions":{
		"target":"ES6",
		"module": "es2015",
		"outDir": "./dist",
	}
}

5.5 outFile

将编译后的文件合并问指定文件

{
	"compilerOptions":{
		"target":"ES6",
		"module": "es2015",
		"outDir": "./dist",
		"outFile": "./dist/index.js"
	}
}

5.6 allowJs

是否允许编译js文件,默认false

{
	"compilerOptions":{
		"target":"ES6",
		"module": "es2015",
		"outDir": "./dist",
		"allowJs": true
	}
}

5.7 checkJs

是否检查js文件

{
	"compilerOptions":{
		"target":"ES6",
		"module": "es2015",
		"outDir": "./dist",
		"allowJs": true,
		"checkJs": true,
	}
}

5.8 removeComments

是否移出注释

{
	"compilerOptions":{
		"target":"ES6",
		"module": "es2015",
		"outDir": "./dist",
		"allowJs": true,
		"checkJs": true,
		"removeComments": true,
	}
}

5.9 onEmit

是否不生成编译后的文件

{
	"compilerOptions":{
		"target":"ES6",
		"module": "es2015",
		"outDir": "./dist",
		"allowJs": true,
		"checkJs": true,
		"removeComments": true,
		"onEmit": false,
	}
}

5.10 onEmitOnError

是否在编译文件出错时停止编译

{
	"compilerOptions":{
		"target":"ES6",
		"module": "es2015",
		"outDir": "./dist",
		"allowJs": true,
		"checkJs": true,
		"removeComments": true,
		"onEmit": false,
		"onEmitOnError":false,
	}
}

5.11 alwaysStrict

是否以严格模式编译

{
	"compilerOptions":{
		"target":"ES6",
		"module": "es2015",
		"outDir": "./dist",
		"allowJs": true,
		"checkJs": true,
		"removeComments": true,
		"onEmit": false,
		"onEmitOnError":false,
		"alwaysStrict":true,
	}
}

5.12 noImplicitAny

是否允许使用any

{
	"compilerOptions":{
		"target":"ES6",
		"module": "es2015",
		"outDir": "./dist",
		"allowJs": true,
		"checkJs": true,
		"removeComments": true,
		"onEmit": false,
		"onEmitOnError":false,
		"alwaysStrict":true,
		"onImplicitAny":false,
	}
}

5.13 noImplicitThis

是否允许使用隐式的this

{
	"compilerOptions":{
		"target":"ES6",
		"module": "es2015",
		"outDir": "./dist",
		"allowJs": true,
		"checkJs": true,
		"removeComments": true,
		"onEmit": false,
		"onEmitOnError":false,
		"alwaysStrict":true,
		"onImplicitAny":false,
		"noImplicitThis":false,
	}
}

5.13 strictNullChecks

是否严格检查Null

{
	"compilerOptions":{
		"target":"ES6",
		"module": "es2015",
		"outDir": "./dist",
		"allowJs": true,
		"checkJs": true,
		"removeComments": true,
		"onEmit": false,
		"onEmitOnError":false,
		"alwaysStrict":true,
		"onImplicitAny":false,
		"noImplicitThis":false,
		"strictNullChecks":true
	}
}

5.14 strict

所有严格检查的总开关

{
	"compilerOptions":{
		"target":"ES6",
		"module": "es2015",
		"outDir": "./dist",
		"allowJs": true,
		"checkJs": true,
		"removeComments": true,
		"onEmit": false,
		"onEmitOnError":false,
		"alwaysStrict":true,
		"onImplicitAny":false,
		"noImplicitThis":false,
		"strictNullChecks":true,
		"strict":true
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
TypeScript配置详解可以通过编辑一个名为tsconfig.json的配置文件来实现。tsconfig.json是TypeScript编译器的配置文件,它允许您指定编译选项和项目设置。以下是一些常见的配置选项: 1. "compilerOptions":这个选项包含了一系列配置项,用于指定编译器的行为。例如,您可以设置"target"来指定要编译到的JavaScript版本,设置"outDir"来指定输出目录,设置"strict"来启用严格的类型检查等等。 2. "include"和"exclude":这两个选项用于指定要包含或排除的文件文件夹。您可以使用通配符模式来匹配文件路径。 3. "files":这个选项允许您直接指定要编译的文件列表。如果您不想使用"include"和"exclude"来匹配文件,可以使用这个选项。 4. "extends":这个选项允许您继承其他配置文件设置。这对于共享通用设置非常有用。 5. "references":这个选项用于指定项目之间的引用关系。可以使用这个选项来构建具有多个项目的解决方案。 通过编辑tsconfig.json文件,您可以根据自己的需求配置TypeScript编译器的行为和项目设置。您可以指定编译选项、包含或排除文件、指定输出目录等等。通过这些配置,您可以更好地控制TypeScript项目的编译过程和行为。 要编译TypeScript代码,您可以使用tsc命令行工具。例如,要编译名为helloTypeScript.ts的TypeScript文件,您可以运行以下命令: tsc helloTypeScript.ts 这将使用tsc编译器将TypeScript代码转换为JavaScript代码。编译后的JavaScript文件将与输入文件位于同一目录中,并且具有相同的文件名,但扩展名为.js。通过这种方式,您可以在TypeScript和JavaScript之间进行无缝切换和集成。 请注意,tsc是TypeScript编译器的缩写,表示TypeScript Compiler。它是用于将TypeScript代码编译为JavaScript代码的命令行工具。您可以通过安装TypeScript来获取tsc编译器,并将其配置到系统或项目的路径中。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [TypeScript环境配置详解](https://blog.csdn.net/qq_44749491/article/details/127419988)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值