TypeScript Utility Types
Extract<T, U> — keep matching union members
Extract distributes over T and keeps constituents assignable to U. It is useful for selecting variants by discriminant or selecting compatible literal types. It does not produce a runtime predicate and it is not a general intersection operator: if a whole constituent is not assignable, it is removed. Example output: "8". Verified with strict TypeScript 5.8.3, ES2022 and Node.js 22.16.0. This utility affects static typing; no runtime validation or transformation is supplied by the type itself.
Formal Syntax & Structure
typescript syntaxtypescript
Extract<T, U>Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| T | type parameter | Source union or type. |
| U | type parameter | Type used to select assignable constituents. |
Working Example
Example Snippettypescript
type Event = { kind: "open"; id: number } | { kind: "close"; reason: string };type OpenEvent = Extract<Event, { kind: "open" }>;const event: OpenEvent = { kind: "open", id: 8 };console.log(event.id);