目录

@typescript-eslint/ban-types

Disallow certain types.

Rule Configuration

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

Options

This rule supports the following options:

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

Example:

"@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
  },
]

Example of Correct Code

// Type casing should remain consistent in lowercase.
const str: string = 'foo';
const bool: boolean = true;
const num: number = 1;
const bigInt: bigint = 1n;

// Use the correct function types.
const func: () => string = () => 'hello';

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

Example of Incorrect Code

// Type casing should remain consistent in lowercase.
const str: String = 'foo';
const bool: Boolean = true;
const num: Number = 1;
const bigInt: BigInt = 1n;

// Use the correct function types.
const func: Function = () => 'hello';

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

Rule Set

plugin:@typescript-eslint/all

For details about how to configure Code Linter check rules, see Code Linter.