Skip to main content

prefer-type-fest-required-keys-of

Require TypeFest RequiredKeysOf<T> over expanded required-key extraction helpers.

Targeted pattern scopeโ€‹

This rule reports the exact TypeFest composition T extends unknown ? Exclude<keyof T, OptionalKeysOf<T>> : never.

It supports direct, aliased, and namespace-qualified TypeFest imports for OptionalKeysOf.

What this rule reportsโ€‹

This rule reports expanded required-key extraction helpers that can be replaced by RequiredKeysOf<T>.

Why this rule existsโ€‹

RequiredKeysOf<T> is the canonical TypeFest utility for extracting required keys. Using it directly avoids repeating TypeFest helper compositions in user code.

โŒ Incorrectโ€‹

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

type Result<Type extends object> = Type extends unknown
? Exclude<keyof Type, OptionalKeysOf<Type>>
: never;

โœ… Correctโ€‹

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

type Result<Type extends object> = RequiredKeysOf<Type>;

Behavior and migration notesโ€‹

  • The OptionalKeysOf reference must resolve to a type-fest import.
  • The rule only reports the exact distributive exclusion shape used by TypeFest.
  • Non-distributive exclusions are ignored because they can differ for union types.
  • Autofix is skipped when RequiredKeysOf is shadowed in the local scope.

Additional examplesโ€‹

โŒ Incorrect โ€” Namespace importโ€‹

import type * as TypeFest from "type-fest";

type Result<Type extends object> = Type extends unknown
? Exclude<keyof Type, TypeFest.OptionalKeysOf<Type>>
: never;

โœ… Correct โ€” Namespace importโ€‹

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

type Result<Type extends object> = RequiredKeysOf<Type>;

โœ… Correct โ€” Non-distributive helperโ€‹

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

type Result<Type extends object> = Exclude<keyof Type, OptionalKeysOf<Type>>;

ESLint flat config exampleโ€‹

import typefest from "eslint-plugin-typefest";

export default [
{
plugins: { typefest },
rules: {
"typefest/prefer-type-fest-required-keys-of": "error",
},
},
];

When not to use itโ€‹

Disable this rule if a public helper must intentionally expose the expanded exclusion implementation.

Package documentationโ€‹

TypeFest package documentation:

Source file: source/required-keys-of.d.ts

Rule catalog ID: R122

Further readingโ€‹

Adoption resourcesโ€‹