Skip to content

useConsistentIndexedObjectStyle (JavaScript)

Language JavaScript (and super languages)
biome.json
{
"linter": {
"rules": {
"nursery": {
"useConsistentIndexedObjectStyle": "error"
}
}
}
}

Require a consistent style for object types with arbitrary keys.

TypeScript offers several ways to write the type of an object whose keys are not known in advance: an index signature ({ [key: string]: unknown }), a mapped type ({ [key in string]: unknown }), or the built-in Record type (Record<string, unknown>). All three describe the same type.

Using one style throughout the code makes these types easier to read. By default, this rule requires the Record type.

The rule doesn’t report object types that refer to themselves, such as interface Tree { [key: string]: Tree }, because TypeScript doesn’t allow a Record type to refer to itself.

interface Scores {
[name: string]: number;
}
code-block.ts:1:1 lint/nursery/useConsistentIndexedObjectStyle  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ Use a Record type instead of an index signature.

> 1 │ interface Scores {
│ ^^^^^^^^^^^^^^^^^^
> 2 │ [name: string]: number;
> 3 │ }
│ ^
4 │

ℹ Using one style for object types with arbitrary keys makes the code easier to read.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

ℹ Unsafe fix: Use a Record type.

1 │ - interface·Scores·{
2 │ - ····[name:·string]:·number;
3 │ - }
1 │ + type·Scores·=·Record<string,·number>;
4 2 │

type Scores = {
[name: string]: number;
};
code-block.ts:1:15 lint/nursery/useConsistentIndexedObjectStyle  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ Use a Record type instead of an index signature.

> 1 │ type Scores = {
│ ^
> 2 │ [name: string]: number;
> 3 │ };
│ ^
4 │

ℹ Using one style for object types with arbitrary keys makes the code easier to read.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

ℹ Unsafe fix: Use a Record type.

1 │ - type·Scores·=·{
2 │ - ····[name:·string]:·number;
3 │ - };
1 │ + type·Scores·=·Record<string,·number>;
4 2 │

type Scores = {
[name in string]: number;
};
code-block.ts:1:15 lint/nursery/useConsistentIndexedObjectStyle  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ Use a Record type instead of a mapped type.

> 1 │ type Scores = {
│ ^
> 2 │ [name in string]: number;
> 3 │ };
│ ^
4 │

ℹ Using one style for object types with arbitrary keys makes the code easier to read.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

ℹ Unsafe fix: Use a Record type.

1 │ - type·Scores·=·{
2 │ - ····[name·in·string]:·number;
3 │ - };
1 │ + type·Scores·=·Record<string,·number>;
4 2 │

type Scores = Record<string, number>;
interface Player {
name: string;
[key: string]: unknown;
}
interface Tree {
[key: string]: Tree;
}

Default: "record"

The style to require:

  • "record": object types with arbitrary keys must use the Record type.
  • "indexSignature": object types with arbitrary keys must use an index signature.
biome.json
{
"linter": {
"rules": {
"nursery": {
"useConsistentIndexedObjectStyle": {
"level": "on",
"options": {
"style": "indexSignature"
}
}
}
}
}
}
type Scores = Record<string, number>;
code-block.ts:1:15 lint/nursery/useConsistentIndexedObjectStyle  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ Use an index signature instead of the Record type.

> 1 │ type Scores = Record<string, number>;
│ ^^^^^^^^^^^^^^^^^^^^^^
2 │

ℹ Using one style for object types with arbitrary keys makes the code easier to read.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

ℹ Unsafe fix: Use an index signature.

1 │ - type·Scores·=·Record<string,·number>;
1 │ + type·Scores·=·{·[key:·string]:·number·};
2 2 │

type Scores = {
[name: string]: number;
};