目录

@typescript-eslint/ban-types

不允许使用某些类型。

规则配置

// code-linter.json5
{
  "rules": {
    "@typescript-eslint/ban-types": "error"
  }
}

选项

支持配置以下选项:

type Options = {
  types: {
    [key: string]: boolean | string | { message: string, fixWith: string } | null;
  };
  extendDefaults: boolean;
}

示例:

"@typescript-eslint/ban-types": [
  "error",
  {
    "types": {
      "String": true,   
      "Number": false,  
      "MyType": "Do not use 'MyType'",  
      "MyTypeWithFix": {
        "message": "Do not use 'MyTypeWithFix', use 'MyType' instead",
        "fixWith": "MyType"
      }
    },
    "extendDefaults": true
  },
]

正例

// 类型小写保持一致
const str: string = 'foo';
const bool: boolean = true;
const num: number = 1;
const bigInt: bigint = 1n;

// 使用正确的函数类型
const func: () => string = () => 'hello';

export { str, bool, num, bigInt, func };

反例

// 类型小写保持一致
const str: String = 'foo';
const bool: Boolean = true;
const num: Number = 1;
const bigInt: BigInt = 1n;

// 使用正确的函数类型
const func: Function = () => 'hello';

export { str, bool, num, bigInt, func };

规则集

plugin:@typescript-eslint/all

Code Linter代码检查规则的配置指导请参考Code Linter代码检查。