typescript/consistent-indexed-object-style Style
What it does
Choose between requiring either Record type or indexed signature types.
These two types are equivalent, this rule enforces consistency in picking one style over the other:
type Foo = Record<string, unknown>;
type Foo = {
[key: string]: unknown;
};Why is this bad?
Inconsistent style for indexed object types can harm readability in a project.
Examples
Examples of incorrect code for this rule with consistent-indexed-object-style: ["error", "record"] (default):
interface Foo {
[key: string]: unknown;
}
type Foo = {
[key: string]: unknown;
};Examples of correct code for this rule with consistent-indexed-object-style: ["error", "record"] (default):
type Foo = Record<string, unknown>;Examples of incorrect code for this rule with consistent-indexed-object-style: ["error", "index-signature"]:
type Foo = Record<string, unknown>;Examples of correct code for this rule with consistent-indexed-object-style: ["error", "index-signature"]:
interface Foo {
[key: string]: unknown;
}
type Foo = {
[key: string]: unknown;
};Configuration
This rule accepts a configuration object with the following properties:
preferredStyle
type: "record" | "index-signature"
default: "record"
When set to record, enforces the use of a Record for indexed object types, e.g. Record<string, unknown>. When set to index-signature, enforces the use of indexed signature types, e.g. { [key: string]: unknown }.
How to use
To enable this rule in the CLI or using the config file, you can use:
oxlint --deny typescript/consistent-indexed-object-style{
"rules": {
"typescript/consistent-indexed-object-style": "error"
}
}