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
OptionalKeysOfreference must resolve to atype-festimport. - The rule only reports the exact
extends never ? false : trueshape. - Inverted checks and custom fallback branches are ignored.
- Autofix is skipped when
HasOptionalKeysis 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