consistent-indexed-object-style
Require or disallow the
Record
type.
🎨
Extending "plugin:@typescript-eslint/stylistic"
in an ESLint configuration enables this rule.
🔧
Some problems reported by this rule are automatically fixable by the --fix
ESLint command line option.
TypeScript supports defining arbitrary object keys 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>;
Keeping to one declaration form consistently improve code readability.
.eslintrc.cjs
module.exports = {
"rules": {
"@typescript-eslint/consistent-indexed-object-style": "error"
}
};
Options
This rule accepts the following options
type Options = ['index-signature' | 'record'];
const defaultOptions: Options = ['record'];
"record"
(default): only allow theRecord
type."index-signature"
: only allow index signatures.
record
- ❌ Incorrect
- ✅ Correct
interface Foo {
[key: string]: unknown;
}
type Foo = {
[key: string]: unknown;
};
Open in Playgroundtype Foo = Record<string, unknown>;
Open in Playgroundindex-signature
- ❌ Incorrect
- ✅ Correct
type Foo = Record<string, unknown>;
Open in Playgroundinterface Foo {
[key: string]: unknown;
}
type Foo = {
[key: string]: unknown;
};
Open in Playground