JS.TS.CONSISTENT.TYPE.DEFINITIONS

Consistent with type definition either 'interface' or 'type'

There are two ways to define a type.

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

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

Options

This rule accepts one string option:

  • "interface": enforce using interfaces for object type definitions.
  • "type": enforce using types for object type definitions.

For example:

コピー
{
  // Use type for object definitions
  "@typescript-eslint/consistent-type-definitions": ["error", "type"]
}

interface

Examples of code with interface option.

<!--tabs-->

Incorrect

コピー
type T = { x: number };

Correct

コピー
type T = string;
type Foo = string | {};

interface T {
  x: number;
}

type

Examples of code with type option.

<!--tabs-->

Incorrect

コピー
interface T {
  x: number;
}

Correct

コピー
type 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.

The content on this page is adapted from the ESLint User Guide. Copyright © OpenJS Foundation and other contributors, www.openjsf.org. All rights reserved. https://eslint.org/docs/rules/