@typescript-eslint/consistent-indexed-object-style
Require either Record<K, V> or an index signature, consistently
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. A type alias can't be declared twice, so an interface whose name is declared again in the file is reported but not fixed. Other files add to an interface in a declaration file, a namespace or a declare module block, so only --fix-dangerously rewrites those.
Configure#
Off by default. To turn it on:
[lint.rules]
"@typescript-eslint/consistent-indexed-object-style" = "error" # or "warn"Every option, set to its default:
[lint.rules]
# "record" or "index-signature"
"@typescript-eslint/consistent-indexed-object-style" = ["error", "record"]Examples#
type Scores = { [player: string]: number }A record is preferred over an index signature.;
interface Prices {A record is preferred over an index signature.
[product: string]: number;
}A mapped type that doesn't use its key is also a Record:
type Flags = { [Name in "debug" | "verbose"]: boolean }A record is preferred over an index signature.;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 }. |
"index-signature"1 problemtype Scores = Record<string, number>An index signature is preferred over a record.;"index-signature"No problemstype Scores = { [player: string]: number };