TypeScript supports defining object show keys can be flexible 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>;
"record"
: Set to"always"
to only allow theRecord
type. Set to"never"
to only allow index signatures. (Defaults to"always"
)
For example:
{
"@typescript-eslint/consistent-type-definitions": ["error", "never"]
}
This rule enforces a consistent way to define records.
Examples of incorrect code with always
option.
interface Foo {
[key: string]: unknown;
}
type Foo = {
[key: string]: unknown;
};
Examples of correct code with always
option.
type Foo = Record<string, unknown>;
Examples of incorrect code with never
option.
type Foo = Record<string, unknown>;
Examples of correct code with never
option.
interface Foo {
[key: string]: unknown;
}
type Foo = {
[key: string]: unknown;
};