Skip to main content

prefer-type-fest-keys-of-union

Require TypeFest KeysOfUnion<T> over imported aliases like AllKeys.

Targeted pattern scopeโ€‹

This rule targets imported alias names used for "all keys across union members" extraction.

What this rule reportsโ€‹

  • Type references that resolve to imported AllKeys aliases.

Why this rule existsโ€‹

KeysOfUnion is the canonical TypeFest utility for extracting the full key union across object unions. Using canonical utility names improves readability and consistency.

โŒ Incorrectโ€‹

import type { AllKeys } from "type-aliases";

type Keys = AllKeys<Foo | Bar>;

โœ… Correctโ€‹

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

type Keys = KeysOfUnion<Foo | Bar>;

Behavior and migration notesโ€‹

  • KeysOfUnion<T> includes keys that appear in any member of an object union.
  • This rule targets alias names with matching semantics (AllKeys).
  • Use this utility when discriminated unions require full key introspection across variants.

Additional examplesโ€‹

โŒ Incorrect โ€” Additional exampleโ€‹

import type { AllKeys } from "type-aliases";

type Keys = AllKeys<A | B>;

โœ… Correct โ€” Additional exampleโ€‹

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

type Keys = KeysOfUnion<A | B>;

โœ… Correct โ€” Repository-wide usageโ€‹

type EventKeys = KeysOfUnion<CreateEvent | DeleteEvent>;

ESLint flat config exampleโ€‹

import typefest from "eslint-plugin-typefest";

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

When not to use itโ€‹

Disable this rule if existing alias names must remain for public API compatibility.

Package documentationโ€‹

TypeFest package documentation:

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

/**
* Create a union of all keys from a given type, even those exclusive to
* specific union members.
*
* Unlike the native `keyof` keyword, which returns keys present in **all**
* union members, this type returns keys from **any** member.
*
* @category Object
*
* @example
* ```
* import type {KeysOfUnion} from 'type-fest';
*
* type A = {
* common: string;
* a: number;
* };
*
* type B = {
* common: string;
* b: string;
* };
*
* type C = {
* common: string;
* c: boolean;
* };
*
* type Union = A | B | C;
*
* type CommonKeys = keyof Union;
* //=> 'common'
*
* type AllKeys = KeysOfUnion<Union>;
* //=> 'common' | 'a' | 'b' | 'c'
* ```;
*
* @link https://stackoverflow.com/a/49402091
*/

Rule catalog ID: R047

Further readingโ€‹

Adoption resourcesโ€‹