美文网首页
tsconfig.json 配置文件选项说明

tsconfig.json 配置文件选项说明

作者: 一蓑烟雨任平生_cui | 来源:发表于2021-03-20 18:09 被阅读0次

1. include

定义被编译文件所在的目录。

默认值: ["**/*"]

** 任意目录

* 任意文件

2. exclude

不需要被编译的文件目录。

默认值:["node_modules", "bower_components", "jspm_packages"]

3. extends

定义被继承的配置文件。

示例:

"extends": "./config.base.json"

4. files

指定被编译的文件列表。一般只有需要编译的文件少时才会用到。

示例:

"files": [
  "index.ts",
  "foo.ts"
]

5. compilerOptions

编译选项。

1. target

指定 TS 被编译为的 ES 版本。

可选值:'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'

2. module

指定要使用的模块化规范。

可选值:'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'

3. lib

用来指定项目中要使用的库。

可选值:'es5', 'es6', 'es2015', 'es7', 'es2016', 'es2017', 'es2018', 'es2019', 'es2020', 'esnext', 'dom', 'dom.iterable', 'webworker', 'webworker.importscripts', 'webworker.iterable', 'scripthost', 'es2015.core', 'es2015.collection', 'es2015.generator', 'es2015.iterable', 'es2015.promise', 'es2015.proxy', 'es2015.reflect', 'es2015.symbol', 'es2015.symbol.wellknown', 'es2016.array.include', 'es2017.object', 'es2017.sharedmemory', 'es2017.string', 'es2017.intl', 'es2017.typedarrays', 'es2018.asyncgenerator', 'es2018.asynciterable', 'es2018.intl', 'es2018.promise', 'es2018.regexp', 'es2019.array', 'es2019.object', 'es2019.string', 'es2019.symbol', 'es2020.bigint', 'es2020.promise', 'es2020.sharedmemory', 'es2020.string', 'es2020.symbol.wellknown', 'es2020.intl', 'esnext.array', 'esnext.symbol', 'esnext.asynciterable', 'esnext.intl', 'esnext.bigint', 'esnext.string', 'esnext.promise', 'esnext.weakref'

4. outDir

指定编译后的文件所在目录。

5. outFile

所有全局作用域中的代码合并为一个文件。

如果 ts 中使用了模块化,则配置文件中 module 需要设置为 system。

6. allowJs

是否对 js 文件进行编译。默认 false。

7. checkJs

是否检查 js 代码符合语法规范。默认 false。

8. removeComments

是否移除注释。默认 false

9. noEmit

不生成编译后的文件。默认 false

10. noEmitOnError

有错误时不生成编译文件。默认 true

11. alwaysStrict

编译后的文件是否设置严格模式。默认 true

12. noImplicitAny

不允许隐式的 any。默认 true

// 隐式any
function fn(a, b) {
  return a + b
}

13. noImplicitThis

不允许不明确类型的 this。默认 true

比如以下函数中的 this。在非严格模式下普通函数调用,this 为 window。严格模式下为 undefined。以上下文方式调用时,this 为其所指向的上下文。

function func() {
  console.log(this)
}

手动指定:

function fn(this: Window) {
  console.log(this)
}

14. strictNullChecks

严格检查空值。默认 true

比如下面情况:box 可能获取到的是 null。null 并没有方法。所以可能会报错。

const box = document.getElementById('box')
box.addEventListener('click', () => {
  // do something...
})

解决方式:先判断是否存在,使用 if 或通过可选链判断。

if (box) {
  // do something...
}
box?.addEventListener('click', () {
  // do something...
})

15. strict

所有严格检查的总开关。默认 true

strict 开启后,所有 strict 相关的全部开启。

相关文章

网友评论

      本文标题:tsconfig.json 配置文件选项说明

      本文链接:https://www.haomeiwen.com/subject/tkiwcltx.html