no-restricted-types | typescript-eslint (original) (raw)

It can sometimes be useful to ban specific types from being used in type annotations. For example, a project might be migrating from using one type to another, and want to ban references to the old type.

This rule can be configured to ban a list of specific types and can suggest alternatives. Note that it does not ban the corresponding runtime objects from being used.

An object whose keys are the types you want to ban, and the values are error messages.

The type can either be a type name literal (OldType) or a a type name with generic parameter instantiation(s) (OldType<MyArgument>).

{
  "@typescript-eslint/no-restricted-types": [
    "error",
    {
      "types": {
        // add a custom message to help explain why not to use it
        "OldType": "Don't use OldType because it is unsafe",

        // add a custom message, and tell the plugin how to fix it
        "OldAPI": {
          "message": "Use NewAPI instead",
          "fixWith": "NewAPI",
        },

        // add a custom message, and tell the plugin how to suggest a fix
        "SoonToBeOldAPI": {
          "message": "Use NewAPI instead",
          "suggest": ["NewAPIOne", "NewAPITwo"],
        },
      },
    },
  ],
}

If you have no need to ban specific types from being used in type annotations, you don't need this rule.