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. 选择完成配置
√ 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
2
3
4
5
6
7
8
9
10
  1. .eslintrc.js中可以看到一个多出来的parser,这个是语法解析器。
// ts对于js有很多的语法,所以要指定一个解析器
parser: '@typescript-eslint/parser',
1
2
  1. index.ts中编写错误代码
function foo(ms: string): void{
  console.log(msg);
}

foo('hello ts~')
1
2
3
4
5
  1. 在命令行中运行npx eslint .\index.ts,可以看到报错信息。
更新时间: 2021-10-11 15:57