Skip to main content

consistent-indexed-object-style

Require or disallow the Record type.

TypeScript supports defining object show keys can be flexible using an index signature. TypeScript also has a builtin type named Record to create an empty object defining only an index signature. For example, the following types are equal:

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

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

type Foo = Record<string, unknown>;

Attributes

  • Included in configs
    • ✅ Recommended
    • 🔒 Strict
  • Fixable
    • 🔧 Automated Fixer
    • 💡 Suggestion Fixer
  • 💭 Requires type information

Rule Details

This rule enforces a consistent way to define records.

.eslintrc.cjs
module.exports = {
"rules": {
"@typescript-eslint/consistent-indexed-object-style": "warn"
}
};

Options

This rule accepts an options string of the following possible values:

type Options = "record" | "index-signature";

const defaultOptions: Options = ["record"];
  • "record" (default): only allow the Record type.
  • "index-signature": only allow index signatures.

record

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

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

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

index-signature

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

type Foo = Record<string, unknown>;

Resources