1、背景
node 项目原本使用 tslint 做代码规范,但是 tslint 不再维护,作者会将精力放在共同优化 eslint 上。
2、解决办法:
去除 tslint,使用 typescript-eslint。
3、具体实现
(1)安装:
yarn add -D eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin
(2)基础配置:
在根目录的 .eslintrc.js 文件中配置如下:
module.exports={root:true,parser:'@typescript-eslint/parser',plugins:['@typescript-eslint',],extends:['eslint:recommended','plugin:@typescript-eslint/eslint-recommended','plugin:@typescript-eslint/recommended',],};
(3)配置不校验文件
类似 .gitignore,可以在根目录创建 .eslintignore 文件
(4)执行命令
eslint.--ext .js,.ts src
既可以校验 js 文件,也可以校验 ts 文件。具体参数配置见 文档
4、扩展配置
现有较流行的打包库:aitbnb-typescript、standard-typescript、prettier,具体配置方法见文档。