Skip to content

ESLint检查TypeScript

对于typescript的lint来说,以前都是使用tslint的工具,之后tslint官方放弃维护也建议大家使用eslint+typescript来实现对代码的校验。

  1. 初始化npm npm init --yes,安装eslint、typescript npm i eslint typescript --save-dev、初始化eslintnpx eslint --init
  2. 选择完成配置
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
  1. .eslintrc.js中可以看到一个多出来的parser,这个是语法解析器。
js
// ts对于js有很多的语法,所以要指定一个解析器
parser: '@typescript-eslint/parser',
  1. index.ts中编写错误代码
js
function foo(ms: string): void{
  console.log(msg);
}

foo('hello ts~')
  1. 在命令行中运行npx eslint .\index.ts,可以看到报错信息。

MIT Licensed