Skip to content

Latest commit

 

History

History
92 lines (63 loc) · 1.44 KB

consistent-indexed-object-style.md

File metadata and controls

92 lines (63 loc) · 1.44 KB

consistent-indexed-object-style

Enforce or disallow the use of the record type.

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>;

Options

  • "record": Set to "record" to only allow the Record type. Set to "index-signature" to only allow index signatures. (Defaults to "record")

For example:

{
  "@typescript-eslint/consistent-indexed-object-style": [
    "error",
    "index-signature"
  ]
}

Rule Details

This rule enforces a consistent way to define records.

record

Examples of code with record option.

❌ Incorrect

interface Foo {
  [key: string]: unknown;
}

type Foo = {
  [key: string]: unknown;
};

✅ Correct

type Foo = Record<string, unknown>;

index-signature

Examples of code with index-signature option.

❌ Incorrect

type Foo = Record<string, unknown>;

✅ Correct

interface Foo {
  [key: string]: unknown;
}

type Foo = {
  [key: string]: unknown;
};

Attributes

  • Configs:
    • ✅ Recommended
    • 🔒 Strict
  • 🔧 Fixable
  • 💭 Requires type information