prefer-type-fest-optional-keys-of
Require TypeFest OptionalKeysOf<T> over expanded optional-key extraction helpers.
Targeted pattern scopeโ
This rule reports the exact TypeFest mapped-key composition that derives optional keys from IsOptionalKeyOf<T, K>.
It supports direct, aliased, and namespace-qualified TypeFest imports for IsOptionalKeyOf.
What this rule reportsโ
This rule reports expanded optional-key extraction helpers that can be replaced by OptionalKeysOf<T>.
Why this rule existsโ
OptionalKeysOf<T> is the canonical TypeFest utility for extracting optional keys. Using it directly avoids repeating a fragile mapped-type helper and makes the type intent easier to scan.
โ Incorrectโ
import type { IsOptionalKeyOf } from "type-fest";
type Result<Type extends object> = Type extends unknown
? (keyof {
[Key in keyof Type as IsOptionalKeyOf<Type, Key> extends false
? never
: Key]: never;
}) &
keyof Type
: never;
โ Correctโ
import type { OptionalKeysOf } from "type-fest";
type Result<Type extends object> = OptionalKeysOf<Type>;
Behavior and migration notesโ
- The
IsOptionalKeyOfreference must resolve to atype-festimport. - The rule only reports the exact distributive mapped-key shape used by TypeFest.
- Custom mapped-key filters and non-distributive helpers are ignored.
- Autofix is skipped when
OptionalKeysOfis 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
? (keyof {
[Key in keyof Type as TypeFest.IsOptionalKeyOf<
Type,
Key
> extends false
? never
: Key]: never;
}) &
keyof Type
: never;
โ Correct โ Namespace importโ
import type { OptionalKeysOf } from "type-fest";
type Result<Type extends object> = OptionalKeysOf<Type>;
โ Correct โ Custom mappingโ
import type { IsOptionalKeyOf } from "type-fest";
type Result<Type extends object> = keyof {
[Key in keyof Type as IsOptionalKeyOf<Type, Key> extends false
? never
: Key]: never;
};
ESLint flat config exampleโ
import typefest from "eslint-plugin-typefest";
export default [
{
plugins: { typefest },
rules: {
"typefest/prefer-type-fest-optional-keys-of": "error",
},
},
];
When not to use itโ
Disable this rule if a public helper must intentionally expose the expanded mapped-type implementation.
Package documentationโ
TypeFest package documentation:
Source file: source/optional-keys-of.d.ts
Rule catalog ID: R121