Skip to main content

prefer-type-fest-pick-index-signature

Prefer PickIndexSignature from type-fest over manual mapped types that keep only index signatures.

This rule lives only in the experimental preset and reports without autofixing.

Targeted pattern scopeโ€‹

This rule focuses on the canonical mapped-type counterpart to PickIndexSignature:

  • { [K in keyof T as {} extends Record<K, unknown> ? K : never]: T[K] }

It intentionally skips the inverse OmitIndexSignature pattern and broader remapping logic.

What this rule reportsโ€‹

This rule reports mapped types when all of the following are true:

  • the key constraint is keyof ObjectType
  • the key remap condition is {} extends Record<Key, unknown> ? Key : never
  • the mapped value is ObjectType[Key]

The rule is currently report-only. It does not autofix or suggest a replacement yet.

Why this rule existsโ€‹

PickIndexSignature<ObjectType> names the intent directly.

  • Readers do not need to decode the Record<Key, unknown> trick.
  • The helper makes it obvious that only index signatures survive.
  • Using the upstream utility keeps this niche pattern consistent across a codebase.

โŒ Incorrectโ€‹

type IndexOnly<ObjectType> = {
[Key in keyof ObjectType as {} extends Record<Key, unknown>
? Key
: never]: ObjectType[Key];
};

โœ… Correctโ€‹

import type {PickIndexSignature} from "type-fest";

type IndexOnly<ObjectType> = PickIndexSignature<ObjectType>;

Behavior and migration notesโ€‹

  • This rule only reports the narrow canonical mapped-type pattern.
  • It intentionally ignores the inverse {} extends Record<Key, unknown> ? never : Key form because that belongs to OmitIndexSignature, not PickIndexSignature.
  • It also ignores broader remapped types that do more than keep index signatures.

ESLint flat config exampleโ€‹

import typefest from "eslint-plugin-typefest";

export default [typefest.configs.experimental];

When not to use itโ€‹

Disable this rule if your team prefers the explicit mapped type or if you want to reserve PickIndexSignature for only a subset of object utility code.

Package documentationโ€‹

TypeFest package documentation:

Source file: source/pick-index-signature.d.ts

import type {PickIndexSignature} from "type-fest";

type IndexOnly<ObjectType> = PickIndexSignature<ObjectType>;

Rule catalog ID: R094

Further readingโ€‹

Adoption resourcesโ€‹