Skip to content

ExtractUnionByKey<U, K>

ts
type ExtractUnionByKey<U, K> = U extends Record<K, unknown> ? U : never;

Defined in: types.ts:84

Extracts members from a union type U that have a specific key K.

Type Parameters

Type ParameterDescription
UThe union type to filter
K extends string | number | symbolThe key that must exist on the extracted types

Example

ts
type Union = { type: 'a'; value: string } | { type: 'b'; count: number } | { other: boolean };
type Result = ExtractUnionByKey<Union, 'type'>;
// Result: { type: 'a'; value: string } | { type: 'b'; count: number }