Skip to content

typescript/consistent-indexed-object-style Style

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

What it does

Choose between requiring either Record type or indexed signature types.

Why is this bad?

Inconsistent style for indexed object types can harm readability in a project.

Examples

Examples of incorrect code for this rule with the default "record":

ts
/*eslint consistent-indexed-object-style: ["error", "record"]*/

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

Examples of correct code for this rule:

ts
/*eslint consistent-indexed-object-style: ["error", "record"]*/

type Foo = Record<string, unknown>;

Examples of incorrect code for this rule with "index-signature":

ts
/*eslint consistent-indexed-object-style: ["error", "index-signature"]*/

type Foo = Record<string, unknown>;

Examples of correct code for this rule:

ts
/*eslint consistent-indexed-object-style: ["error", "index-signature"]*/

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

Configuration

This rule accepts a configuration object with the following properties:

isRecordMode

type: boolean

default: true

When set to true, enforces the use of Record type for indexed object types. When set to false, enforces the use of indexed signature types.

How to use

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

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

References

Released under the MIT License.