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
AllKeysaliases.
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.
@link https://stackoverflow.com/a/49402091
@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'
```
@category Object
*/
Rule catalog ID: R047