typescript-eslint详解

1,455 阅读6分钟

携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第1天,点击查看活动详情

@typescript-eslint/no-empty-interface 声明有意义的接口

  '@typescript-eslint/no-empty-interface': ['error', {
    allowSingleExtends: true
  }],
bad
// an empty interface
interface Foo {}

good
interface Foo {
  name: string
}
  • allowSingleExtends: true将在不添加其他成员的情况下消除有关扩展单个接口的警告
`allowSingleExtends: true` 下:
forbid
interface Bar extends Foo {}

allow
interface Bar extends Foo {}

建议使用allowSingleExtends: true

@typescript-eslint/prefer-nullish-coalescing 强制使用无效合并运算符而不是逻辑链接

  '@typescript-eslint/prefer-nullish-coalescing': ['error', {
    ignoreConditionalTests: false,
    ignoreMixedLogicalExpressions: false
  }],
  

ignoreConditionalTests

将导致规则忽略位于条件测试中的任何案例

ignoreConditionalTests: false 下

declare const a: string | null;
declare const b: string | null;

bad
if (a || b) {
}

good
if (a ?? b) {
}

ignoreMixedLogicalExpressions

规则是否忽略位于混合表达式逻辑

ignoreMixedLogicalExpressions: false 下

declare const a: string | null;
declare const b: string | null;
declare const c: string | null;
declare const d: string | null;

bad
(a && b) || c || d;

good
(a && b) ?? c ?? d;

建议: ignoreConditionalTests: false, ignoreMixedLogicalExpressions: false

@typescript-eslint/naming-convention 对代码库中的所有内容强制执行命名约定

  '@typescript-eslint/naming-convention': ['error', {
      selector: 'variableLike',
      leadingUnderscore: 'allow',  // 前置下划线
      trailingUnderscore: 'allow', // 后置下划线
      format: ['camelCase', 'PascalCase', 'UPPER_CASE']
  }],
type Options = {
  // format options
  format:
    | (
        | 'camelCase'
        | 'strictCamelCase'
        | 'PascalCase'
        | 'StrictPascalCase'
        | 'snake_case'
        | 'UPPER_CASE'
      )[]
    | null;
  custom?: {
    regex: string;
    match: boolean;
  };
  leadingUnderscore?:
    | 'forbid'
    | 'require'
    | 'requireDouble'
    | 'allow'
    | 'allowDouble'
    | 'allowSingleOrDouble';
  trailingUnderscore?:
    | 'forbid'
    | 'require'
    | 'requireDouble'
    | 'allow'
    | 'allowDouble'
    | 'allowSingleOrDouble';
  prefix?: string[];
  suffix?: string[];

  // selector options
  selector: Selector | Selector[];
  filter?:
    | string
    | {
        regex: string;
        match: boolean;
      };
  // the allowed values for these are dependent on the selector - see below
  modifiers?: Modifiers<Selector>[];
  types?: Types<Selector>[];
}[];

format

选项定义了标识符的允许格式。此选项接受以下值的数组,标识符可以匹配其中任何一个:

  • camelCase- 标准camelCase 格式- 字符之间不允许有下划线,并且允许连续大写(即两者myIDmyId有效)。
  • strictCamelCase- 与 相同camelCase,但不允许连续大写(即myId有效,但myID无效)。
  • PascalCase- 与 相同camelCase,但第一个字符必须大写。
  • StrictPascalCase- 与 相同strictCamelCase,但第一个字符必须大写。
  • snake_case- 标准蛇大小写格式 - 所有字符必须小写,并且允许使用下划线。
  • UPPER_CASE- 与 相同snake_case,但所有字符必须大写。

custom

custom选项定义了标识符必须(或不能)匹配的自定义正则表达式。此选项允许您对标识符进行更细粒度的控制,让您禁止(或强制)某些模式和子字符串。接受具有以下属性的对象:

  • regex- 一个字符串,然后传递给 RegExp 以创建一个新的正则表达式:new RegExp(regex)
  • match- 如果标识符必须匹配,则为 true,如果标识符不能匹配regex,则为 false 。

leadingUnderscore trailingUnderscore

  • forbid- 根本不允许使用前导/尾随下划线。
  • require- 必须包含一个前导/尾随下划线。
  • requireDouble- 必须包含两个前导/尾随下划线。
  • allow- 没有明确强制存在单个前导/尾随下划线。
  • allowDouble- 没有明确强制存在双前导/尾随下划线。
  • allowSingleOrDouble- 没有明确强制存在单个或双前导/尾随下划线。

selector 个人选择器: 接受一个或一组选择器来定义适用于一个或多个选择器的选项块。

  • variable-匹配任何var let const 变量名。

    • 允许 modifiersconstdestructuredglobalexported
    • 允许 typesbooleanstringnumberfunctionarray
  • function- 匹配任何命名函数声明或命名函数表达式。

    • 允许modifiersglobalexportedunused.
    • 允许types:无。
  • parameter- 匹配任何函数参数。与参数属性不匹配。

    • 允许modifiersdestructuredunused
    • 允许typesbooleanstringnumberfunctionarray.
  • classProperty- 匹配任何类属性。不匹配具有直接函数表达式或箭头函数表达式值的属性。

    • 允许modifiersabstractprivateprotectedpublicreadonlyrequiresQuotesstatic.
    • 允许typesbooleanstringnumberfunctionarray.
  • objectLiteralProperty- 匹配任何对象文字属性。不匹配具有直接函数表达式或箭头函数表达式值的属性。

    • 允许modifierspublicrequiresQuotes
    • 允许typesbooleanstringnumberfunctionarray.
  • typeProperty- 匹配任何对象类型属性。不匹配具有直接函数表达式或箭头函数表达式值的属性。

    • 允许modifierspublicreadonlyrequiresQuotes.
    • 允许typesbooleanstringnumberfunctionarray.
  • parameterProperty- 匹配任何参数属性。

    • 允许modifiersprivateprotectedpublicreadonly.
    • 允许typesbooleanstringnumberfunctionarray.
  • classMethod- 匹配任何类方法。还匹配具有直接函数表达式或箭头函数表达式值的属性。不匹配访问器。

    • 允许modifiersabstractprivateprotectedpublicrequiresQuotesstatic.
    • 允许types:无。
  • objectLiteralMethod- 匹配任何对象字面量方法。还匹配具有直接函数表达式或箭头函数表达式值的属性。不匹配访问器。

    • 允许modifierspublicrequiresQuotes
    • 允许types:无。
  • typeMethod- 匹配任何对象类型方法。还匹配具有直接函数表达式或箭头函数表达式值的属性。不匹配访问器。

    • 允许modifierspublicrequiresQuotes
    • 允许types:无。
  • accessor- 匹配任何访问器。

    • 允许modifiersabstractprivateprotectedpublicrequiresQuotesstatic.
    • 允许typesbooleanstringnumberfunctionarray.
  • enumMember- 匹配任何枚举成员。

    • 允许modifiersrequiresQuotes
    • 允许types:无。
  • class- 匹配任何类声明。

    • 允许modifiersabstractexportedunused.
    • 允许types:无。
  • interface- 匹配任何接口声明。

    • 允许modifiersexportedunused
    • 允许types:无。
  • typeAlias- 匹配任何类型别名声明。

    • 允许modifiersexportedunused
    • 允许types:无。
  • enum- 匹配任何枚举声明。

    • 允许modifiersexportedunused
    • 允许types:无。
  • typeParameter- 匹配任何泛型类型参数声明。

    • 允许modifiersunused
    • 允许types:无。

modifiers允许您指定要精细应用的修饰符,例如可访问性 ( privatepublicprotected),或者是否为static等。

types允许您指定要匹配的类型。此选项仅支持简单的原始类型 ( booleanstringnumberarrayfunction)。

typescript-eslint.io/rules/namin…

为了方便使用退出了分组选择器 default- 匹配一切

variableLike- 与variable,function, parameter

memberLike- 匹配相同的propertyparameterPropertymethodaccessorenumMember

typeLike- 匹配相同的classinterfacetypeAliasenumtypeParameter

property- 与classPropertyobjectLiteralProperty, typeProperty

method- 与classMethodobjectLiteralMethod``typeMethod

// the default config is similar to ESLint's camelcase rule but more strict
const defaultOptions: Options = [
  {
    selector: 'default',
    format: ['camelCase'],
    leadingUnderscore: 'allow',
    trailingUnderscore: 'allow',
  },

  {
    selector: 'variable',
    format: ['camelCase', 'UPPER_CASE'],
    leadingUnderscore: 'allow',
    trailingUnderscore: 'allow',
  },

  {
    selector: 'typeLike',
    format: ['PascalCase'],
  },
];

例子:变量,函数,函数参数不允许前后下划线,并且使用驼峰或者全大写命名,类,接口,类型别名,枚举,类型参数不允许前后下划线,驼峰命名,但是第一个字母必须大写。

{
  "camelcase": "off",
  "@typescript-eslint/naming-convention": [
    "error",
    {
      "selector": "variableLike",
      "leadingUnderscore": 'forbid',  // 前置下划线
      "trailingUnderscore": 'forbid', // 后置下划线
      "format": ["camelCase", "UPPER_CASE"]
    },
    {
      "selector": "typeLike",
      "leadingUnderscore": 'forbid',  // 前置下划线
      "trailingUnderscore": 'forbid', // 后置下划线
      "format": ["PascalCase"]
    }
  ]
}

强制所有 const 变量都是全大写

{
  "@typescript-eslint/naming-convention": [
    "error",
    {
      "selector": "variable",
      "modifiers": ["const"],
      "format": ["UPPER_CASE"]
    }
  ]
}

强制类型参数使用(泛型)T开头

{
  "@typescript-eslint/naming-convention": [
    "error",
    {
      "selector": "typeParameter",
      "format": ["PascalCase"],
      "prefix": ["T"]
    }
  ]
}

排序:

每个标识符都应与一个选择器完全匹配。它可以匹配多个组选择器 - 但只能匹配一个选择器。考虑到这一点 - 基本排序顺序是:

  1. 个人选择器
  2. 分组选择器
  3. 默认选择器