当前位置:  首页>> 技术小册>> TypeScript入门指南

TypeScript提供了类型系统、类、接口等特性,可以帮助我们更好地管理和维护代码。但是 TypeScript 本身并不能完全解决代码质量问题,例如代码风格、变量命名、代码复杂度等问题。这时候就需要 ESLint 来进行代码检查和规范。


ESLint 是一个插件化的 JavaScript 代码检查工具,它可以根据配置文件和插件来定义规则集,检查代码的错误、风格和潜在问题,帮助我们提高代码质量和可读性。

在使用 TypeScript 时,我们可以使用 @typescript-eslint/parser 和 @typescript-eslint/eslint-plugin 插件来扩展 ESLint 的功能,从而实现对 TypeScript 代码的检查和规范。

配置

在使用 @typescript-eslint/parser 和 @typescript-eslint/eslint-plugin 插件之前,我们需要先安装它们:

  1. npm install --save-dev @typescript-eslint/parser @typescript-eslint/eslint-plugin

然后在 .eslintrc.js 配置文件中添加以下内容:

  1. module.exports = {
  2. parser: '@typescript-eslint/parser',
  3. plugins: ['@typescript-eslint'],
  4. extends: ['plugin:@typescript-eslint/recommended'],
  5. };

这里我们配置了 parser 为 @typescript-eslint/parser,表示使用 TypeScript 解析器,plugins 为 @typescript-eslint,表示使用 TypeScript 插件,extends 为 plugin:@typescript-eslint/recommended,表示使用 @typescript-eslint 推荐的规则集。

同时,我们可以添加自定义的规则集,例如:

  1. module.exports = {
  2. parser: '@typescript-eslint/parser',
  3. plugins: ['@typescript-eslint'],
  4. extends: ['plugin:@typescript-eslint/recommended', 'plugin:prettier/recommended'],
  5. rules: {
  6. '@typescript-eslint/explicit-module-boundary-types': 'off',
  7. '@typescript-eslint/no-unused-vars': ['error', { argsIgnorePattern: '^_' }],
  8. },
  9. };

这里我们添加了 prettier 规则集,同时禁用了 @typescript-eslint/explicit-module-boundary-types 规则和修改了 @typescript-eslint/no-unused-vars 规则,忽略了参数名称为 _ 开头的未使用变量。

规则集

ESLint 和 @typescript-eslint/eslint-plugin 支持大量的规则集,涵盖了代码风格、错误检查、最佳实践等方面。下面列举一些常用的规则集及其用法。

@typescript-eslint/no-explicit-any
禁止使用 any 类型,any 类型会使代码丧失类型安全性,增加调试难度。例如:

  1. function foo(arg: any) {
  2. // ...
  3. }

将会被报错。

@typescript-eslint/explicit-function-return-type
要求函数返回值的类型必须显式声明,避免隐式类型推断带来的错误和混乱。例如:

  1. function sum(a: number, b: number): number {
  2. return a + b;
  3. }

会被要求加上返回类型。

@typescript-eslint/no-unused-vars
禁止定义未使用的变量,避免代码冗余和浪费。例如:

  1. const foo = 'bar';

会被报错。

@typescript-eslint/no-use-before-define
禁止在变量定义之前使用变量,避免出现不可预期的行为。例如:

  1. foo();
  2. const foo = () => {
  3. // ...
  4. }

将会被报错。

@typescript-eslint/no-empty-interface
禁止定义空的接口,避免出现无意义的代码。例如:

  1. interface IEmpty {}

会被报错。

@typescript-eslint/no-var-requires
禁止使用 require 语句引入模块,而是应该使用 import 语句。例如:

  1. const module = require('./module');

会被报错。

@typescript-eslint/member-delimiter-style
要求对象和接口的成员之间必须有分号分隔,或者没有分号分隔。例如:

  1. interface IMyInterface {
  2. foo: string;
  3. bar(): void;
  4. }

会被要求加上分号。

@typescript-eslint/indent
要求缩进使用指定的空格数,避免出现混乱的缩进。例如:

  1. function foo() {
  2. ∙∙console.log('foo');
  3. }

会被要求使用 2 个空格缩进。

示例
下面给出一个 TypeScript 代码示例:

  1. interface IMyInterface {
  2. foo: string;
  3. bar(): void;
  4. }
  5. class MyClass implements IMyInterface {
  6. foo = 'bar';
  7. public bar() {
  8. console.log(this.foo);
  9. }
  10. }
  11. const myObject: IMyInterface = new MyClass();
  12. myObject.bar();

运行 ESLint 可以发现没有报错和警告,符合我们的规范要求。

小结
ESLint 可以帮助我们在 TypeScript 代码中检查和规范代码风格、错误检查、最佳实践等方面,帮助我们提高代码质量和可读性。使用 @typescript-eslint/parser 和 @typescript-eslint/eslint-plugin 插件,可以扩展 ESLint 的功能,使其支持 TypeScript 代码的检查和规范。同时,我们可以根据自己的需求,添加或修改规则集,以符合项目的规范要求。


该分类下的相关小册推荐: