Skip to content
← Back to rules

typescript/consistent-indexed-object-style スタイル

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

何ができるか

Record 型かインデックス署名型のどちらかを要求するように選択します。

これらの2つの型は同等ですが、このルールは一方のスタイルを他方よりも優先して一貫性を確保します:

ts
type Foo = Record<string, unknown>;

type Foo = {
  [key: string]: unknown;
};

なぜ問題なのか

インデックス付きオブジェクト型のスタイルが一貫していないと、プロジェクト内の可読性に悪影響を与える可能性があります。

consistent-indexed-object-style: ["error", "record"](デフォルト)での誤りの例:

ts
interface Foo {
  [key: string]: unknown;
}
type Foo = {
  [key: string]: unknown;
};

consistent-indexed-object-style: ["error", "record"](デフォルト)での正しい例:

ts
type Foo = Record<string, unknown>;

consistent-indexed-object-style: ["error", "index-signature"]での誤りの例:

ts
type Foo = Record<string, unknown>;

consistent-indexed-object-style: ["error", "index-signature"]での正しい例:

ts
interface Foo {
  [key: string]: unknown;
}
type Foo = {
  [key: string]: unknown;
};

設定

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

"record"

record に設定された場合、インデックス付きオブジェクト型では Record の使用を強制します。たとえば Record<string, unknown> です。

"index-signature"

index-signature に設定された場合、インデックス署名型の使用を強制します。たとえば { [key: string]: unknown } です。

使用方法

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

json
{
  "rules": {
    "typescript/consistent-indexed-object-style": "error"
  }
}
bash
oxlint --deny typescript/consistent-indexed-object-style

参照