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.

These two types are equivalent, this rule enforces consistency in picking one style over the other:

ts
type Foo = Record<string, unknown>;

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

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 consistent-indexed-object-style: ["error", "record"] (default):

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

Examples of correct code for this rule with consistent-indexed-object-style: ["error", "record"] (default):

ts
type Foo = Record<string, unknown>;

Examples of incorrect code for this rule with consistent-indexed-object-style: ["error", "index-signature"]:

ts
type Foo = Record<string, unknown>;

Examples of correct code for this rule with consistent-indexed-object-style: ["error", "index-signature"]:

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

Configuration

This rule accepts a configuration object with the following properties:

preferredStyle

type: "record" | "index-signature"

default: "record"

When set to record, enforces the use of a Record for indexed object types, e.g. Record<string, unknown>. When set to index-signature, enforces the use of indexed signature types, e.g. { [key: string]: unknown }.

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.