useConsistentIndexedObjectStyle (JavaScript)
Language JavaScript (and super languages)
Summary
Section titled “Summary”- Rule available since:
v2.6.9 - Diagnostic Category:
lint/nursery/useConsistentIndexedObjectStyle - This rule has an unsafe fix.
- The default severity of this rule is information.
- Sources:
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "useConsistentIndexedObjectStyle": "error" } } }}Description
Section titled “Description”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.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”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;}Options
Section titled “Options”Default: "record"
The style to require:
"record": object types with arbitrary keys must use theRecordtype."indexSignature": object types with arbitrary keys must use an index signature.
{ "linter": { "rules": { "nursery": { "useConsistentIndexedObjectStyle": { "level": "on", "options": { "style": "indexSignature" } } } } }}Invalid
Section titled “Invalid”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;};See Also
Section titled “See Also”- If you want to require
interfaceortypefor object types, seeuseConsistentTypeDefinitions.
Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.