Skip to content

typescript/consistent-type-definitions Style

🛠️ An auto-fix is available for this rule.

What it does

Enforce type definitions to consistently use either interface or type.

Why is this bad?

TypeScript provides two common ways to define an object type: interface and type. The two are generally very similar, and can often be used interchangeably. Using the same type declaration style consistently helps with code readability.

Examples

By default this rule enforces the use of interfaces for object types.

Examples of incorrect code for this rule:

typescript
type T = { x: number };

Examples of correct code for this rule:

typescript
type T = string;
type Foo = string | {};

interface T {
  x: number;
}

Options

This rule has a single string option:

{ type: string, default: "interface" }

interface

This is the default option.

type

Enforces the use of types for object type definitions.

Examples of incorrect code for this option:

typescript
interface T {
  x: number;
}

Examples of correct code for this option:

typescript
type T = { x: number };

How to use

To enable this rule in the CLI or using the config file, you can use:

bash
oxlint --deny typescript/consistent-type-definitions
json
{
  "rules": {
    "typescript/consistent-type-definitions": "error"
  }
}

References

Released under the MIT License.