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 Parameter | Description |
|---|---|
U | The union type to filter |
K extends string | number | symbol | The 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 }