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.
keyof
https://stackoverflow.com/a/49402091
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' Copy
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'
Create a union of all keys from a given type, even those exclusive to specific union members.
Unlike the native
keyofkeyword, which returns keys present in all union members, this type returns keys from any member.