Skip to main content

consistent-type-definitions

Enforce type definitions to consistently use either interface or type.

🔒

Extending "plugin:@typescript-eslint/strict" in an ESLint configuration enables this rule.

🛠

Some problems reported by this rule are automatically fixable by the --fix ESLint command line option.

TypeScript provides two common ways to define an object type: interface and type.

// type alias
type T1 = {
a: string;
b: number;
};

// interface keyword
interface T2 {
a: string;
b: number;
}

The two are generally very similar, and can often be used interchangeably. Using the same type declaration style consistently helps with code readability.

.eslintrc.cjs
module.exports = {
"rules": {
"@typescript-eslint/consistent-type-definitions": "warn"
}
};

Options

This rule accepts an options string of the following possible values:

type Options = "interface" | "type";

const defaultOptions: Options = ["interface"];
  • "interface" (default): enforce using interfaces for object type definitions.
  • "type": enforce using types for object type definitions.

interface

/* eslint @typescript-eslint/consistent-type-definitions: ["error", "interface"] */

type T = { x: number };

type

/* eslint @typescript-eslint/consistent-type-definitions: ["error", "type"] */

interface T {
x: number;
}

When Not To Use It

If you specifically want to use an interface or type literal for stylistic reasons, you can disable this rule.

Resources