@typescript-eslint/eslint-plugin
Version:
TypeScript plugin for ESLint
90 lines (63 loc) • 1.85 kB
text/mdx
---
description: 'Require or disallow the `Record` type.'
---
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
> 🛑 This file is source code, not the primary documentation location! 🛑
>
> See **https://typescript-eslint.io/rules/consistent-indexed-object-style** for documentation.
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:
```ts
interface Foo {
[key: string]: unknown;
}
type Foo = {
[key: string]: unknown;
};
type Foo = Record<string, unknown>;
```
Using one declaration form consistently improves code readability.
## Options
- `"record"` _(default)_: only allow the `Record` type.
- `"index-signature"`: only allow index signatures.
### `record`
<Tabs>
<TabItem value="❌ Incorrect">
```ts option='"record"'
interface Foo {
[key: string]: unknown;
}
type Foo = {
[key: string]: unknown;
};
```
</TabItem>
<TabItem value="✅ Correct">
```ts option='"record"'
type Foo = Record<string, unknown>;
```
</TabItem>
</Tabs>
### `index-signature`
<Tabs>
<TabItem value="❌ Incorrect">
```ts option='"index-signature"'
type Foo = Record<string, unknown>;
```
</TabItem>
<TabItem value="✅ Correct">
```ts option='"index-signature"'
interface Foo {
[key: string]: unknown;
}
type Foo = {
[key: string]: unknown;
};
```
</TabItem>
</Tabs>
## When Not To Use It
This rule is purely a stylistic rule for maintaining consistency in your project.
You can turn it off if you don't want to keep a consistent style for indexed object types.
However, keep in mind that inconsistent style can harm readability in a project.
We recommend picking a single option for this rule that works best for your project.