Skip to content

10. 代码检查与编译选项

代码检查

2019年1月,ts官方eslint:typescript-eslint

配置vscode eslint

js
{
  "eslint.validate": [
    "javascript",
    "javascriptreact",
    "typescript"
  ],
  "typescript.tsdk": "node_modules/typescript/lib"
}

// 自动修复
{
    "eslint.autoFixOnSave": true,
    "eslint.validate": [
        "javascript",
        "javascriptreact",
        {
            "language": "typescript",
            "autoFix": true
        },
    ],
    "typescript.tsdk": "node_modules/typescript/lib"
}

编译选项

allowJs

允许编译 js 文件。设置为 true 时,js 文件会被 tsc 编译,否则不会。一般在项目中 js, ts 混合开发时需要设置。

sh
# 设置为 true 时,编译后的文件包含 foo.js
├── lib
   ├── foo.js
   └── index.js
├── src
   ├── foo.js
   └── index.ts
├── package.json
└── tsconfig.json

# 设置为 false 时,编译后的文件不包含 foo.js
├── lib
   └── index.js
├── src
   ├── foo.js
   └── index.ts
├── package.json
└── tsconfig.json

allowSyntheticDefaultImports

允许对不包含默认导出的模块使用默认导入。这个选项不会影响生成的代码,只会影响类型检查。

js
export = foo // 是 ts 为了兼容 commonjs 创造的语法,它对应于 commonjs 中的 module.exports = foo
// 在 ts 中,如果要引入一个通过 export = foo 导出的模块
// 标准的语法是 import foo = require('foo'),或者 import * as foo from 'foo'
// 但习惯是 import foo from 'foo'
// 如果allowSyntheticDefaultImports设置为false,`import foo from 'foo'` 会报错,为了解决这个问题,需要开启这个选项

Released under the MIT License.