Skip to content
← Back to rules

typescript/consistent-type-definitions スタイル

⚠️🛠️ A dangerous auto-fix is available for this rule for some violations.

何をするか

オブジェクト型の定義に一貫して interface または type のどちらかを使用することを強制します。

なぜ悪いのか

TypeScript では、オブジェクト型を定義する2つの一般的な方法が提供されています:interfacetype。 これら2つは基本的に非常に似ており、多くの場合互換的に使用できます。 同じ型宣言スタイルを一貫して使うことで、コードの可読性が向上します。

デフォルトでは、このルールはオブジェクト型の定義に interface を使用することを強制します。

このルールに違反する誤ったコードの例:

typescript
type T = { x: number };

このルールに準拠する正しいコードの例:

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

interface T {
  x: number;
}

設定

このルールは以下の文字列値のいずれかを受け入れます:

"interface"

オブジェクト型の定義において interfacetype 优先する:

typescript
interface T {
  x: number;
}

"type"

オブジェクト型の定義において typeinterface 优先する:

typescript
type T = { x: number };

使用方法

設定ファイルまたは CLI でこのルールを有効化するには、次のように使用できます:

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

参照