API reference › @evolu/common › Types › ExtractType
type ExtractType<TUnion, TType> = Extract<
TUnion,
{
type: TType;
}
>;
Defined in: packages/common/src/Types.ts:358
Extracts members of a discriminated union by their type literal.
Constrains TType to valid type values, so typos fail at the type argument
instead of silently producing never.
Example
type Message =
| { readonly type: "Create"; readonly id: string }
| { readonly type: "Delete"; readonly id: string };
type CreateMessage = ExtractType<Message, "Create">;
// { readonly type: "Create"; readonly id: string }
// Type error: "Cretae" is not a valid Message type
type _Typo = ExtractType<Message, "Cretae">;