Skip to main content

prefer-type-fest-has-optional-keys

Require TypeFest HasOptionalKeys<T> over OptionalKeysOf<T> emptiness checks.

Targeted pattern scopeโ€‹

This rule reports exact OptionalKeysOf<T> extends never ? false : true checks when OptionalKeysOf is imported from type-fest.

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

What this rule reportsโ€‹

This rule reports manual optional-key existence checks that can be replaced by HasOptionalKeys<T>.

Why this rule existsโ€‹

HasOptionalKeys<T> makes optional-key existence intent explicit and avoids repeating TypeFest helper compositions in user code.

โŒ Incorrectโ€‹

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

type Result<T extends object> = OptionalKeysOf<T> extends never ? false : true;

โœ… Correctโ€‹

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

type Result<T extends object> = HasOptionalKeys<T>;

Behavior and migration notesโ€‹

  • The OptionalKeysOf reference must resolve to a type-fest import.
  • The rule only reports the exact extends never ? false : true shape.
  • Inverted checks and custom fallback branches are ignored.
  • Autofix is skipped when HasOptionalKeys is shadowed in the local scope.

Additional examplesโ€‹

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

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

type Result<T extends object> = TypeFest.OptionalKeysOf<T> extends never ? false : true;

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

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

type Result<T extends object> = HasOptionalKeys<T>;

โœ… Correct โ€” Inverted checkโ€‹

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

type Result<T extends object> = OptionalKeysOf<T> extends never ? true : false;

ESLint flat config exampleโ€‹

import typefest from "eslint-plugin-typefest";

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

When not to use itโ€‹

Disable this rule if a public helper must keep the expanded TypeFest composition for compatibility or documentation reasons.

Package documentationโ€‹

TypeFest package documentation:

Source file: source/has-optional-keys.d.ts

Rule catalog ID: R117

Further readingโ€‹

Adoption resourcesโ€‹