@typescript-eslint/consistent-indexed-object-style

Require either Record<K, V> or an index signature, consistently

fixable

Also called typescript-eslint/consistent-indexed-object-style, typescript/consistent-indexed-object-style

Record<string, number> and { [key: string]: number } describe the same object: any string key, with a number as its value. This rule picks one way to write it. By default it's Record.

--fix rewrites the type to the other form where that's safe.

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"@typescript-eslint/consistent-indexed-object-style" = "error" # or "warn"

Every option, set to its default:

bunfig.toml
[lint.rules]
# "record" or "index-signature"
"@typescript-eslint/consistent-indexed-object-style" = ["error", "record"]

Examples#

example.ts2 problems
type Scores = { [player: string]: number };

interface Prices {
  [product: string]: number;
}

A mapped type that doesn't use its key is also a Record:

example.ts1 problem
type Flags = { [Name in "debug" | "verbose"]: boolean };
example.tsNo problems
type Scores = Record<string, number>;
type Prices = Record<string, number>;
type Flags = Record<"debug" | "verbose", boolean>;

An index signature next to other members can't be a Record. Neither can a type that refers to itself, because a type alias for Record can't be circular:

example.tsNo problems
interface Headers {
  [name: string]: string;
  host: string;
}

type Tree = { [name: string]: Tree };

Options#

OptionWhat it does
"record" (default)Require Record<K, V>.
"index-signature"Require { [key: K]: V }.
example.tswith "index-signature"1 problem
type Scores = Record<string, number>;
example.tswith "index-signature"No problems
type Scores = { [player: string]: number };