KeysOfUnion

Get all keys from a union of object types.

#types #typescript #utility-type
export type KeysOfUnion<T> = T extends T ? keyof T : never;
// Usage
type A = { a: string; shared: number };
type B = { b: boolean; shared: number };
type AllKeys = KeysOfUnion<A | B>;
// 'a' | 'b' | 'shared'

Share this snippet

Comments