Appearance
ESLint检查TypeScript
对于typescript的lint来说,以前都是使用tslint的工具,之后tslint官方放弃维护也建议大家使用eslint+typescript来实现对代码的校验。
- 初始化npm
npm init --yes
,安装eslint、typescriptnpm i eslint typescript --save-dev
、初始化eslintnpx eslint --init
- 选择完成配置
bash
√ How would you like to use ESLint? · style
√ What type of modules does your project use? · none
√ Which framework does your project use? · none
# 这里说是否要使用typescript选择yes
√ Does your project use TypeScript? · No / Yes
√ Where does your code run? · browser
√ How would you like to define a style for your project? · guide
√ Which style guide do you want to follow? · standard
√ What format do you want your config file to be in? · JavaScript
√ Would you like to install them now with npm? · Yes
.eslintrc.js
中可以看到一个多出来的parser,这个是语法解析器。
js
// ts对于js有很多的语法,所以要指定一个解析器
parser: '@typescript-eslint/parser',
- 在
index.ts
中编写错误代码
js
function foo(ms: string): void{
console.log(msg);
}
foo('hello ts~')
- 在命令行中运行
npx eslint .\index.ts
,可以看到报错信息。