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
Reported:
type Scores = { [player: string]: number };
interface Prices {
[product: string]: number;
}
A mapped type that doesn't use its key is also a Record:
type Flags = { [Name in "debug" | "verbose"]: boolean };
Not reported:
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:
interface Headers {
[name: string]: string;
host: string;
}
type Tree = { [name: string]: Tree };
Options
| Option | What it does |
|---|---|
"record" (default) | Require Record<K, V>. |
"index-signature" | Require { [key: K]: V }. |
type Scores = Record<string, number>;
type Scores = { [player: string]: number };