0
0
Typescriptprogramming~10 mins

Extract type in Typescript - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Extract type
Define union type
Use Extract<Type, Union>
Check each member of Type
Keep members assignable to Union
Result: new union with extracted members
Extract type takes two types and keeps only those members from the first type that are assignable to the second.
Execution Sample
Typescript
type Fruit = "apple" | "banana" | "orange";
type Citrus = Extract<Fruit, "orange" | "lemon">;
// Citrus is "orange"
Extracts only the members from Fruit that are also in the union "orange" | "lemon".
Execution Table
StepMember CheckedIs Member in Extract Type?ReasonResulting Extract Type
1"apple"No"apple" is not assignable to "orange" | "lemon""" (empty)
2"banana"No"banana" is not assignable to "orange" | "lemon""" (empty)
3"orange"Yes"orange" is assignable to "orange" | "lemon""orange"
4EndN/AAll members checked"orange"
💡 All union members checked; only "orange" is assignable to "orange" | "lemon".
Variable Tracker
VariableStartAfter 1After 2After 3Final
ExtractedType"" (empty)"" (empty)"" (empty)"orange""orange"
Key Moments - 3 Insights
Why is "apple" not included in the Extract type?
"apple" is not assignable to the union "orange" | "lemon", so it is excluded as shown in execution_table row 1.
What happens if none of the members match the Extract condition?
The resulting Extract type becomes empty (never), as shown after checking "apple" and "banana" in rows 1 and 2.
Can Extract be used with non-union types?
Extract works by checking assignability; if the second type is not a union, it returns the type if assignable, else never.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the Extract type after checking "banana"?
A"" (empty)
B"orange"
C"banana"
D"apple" | "banana"
💡 Hint
Check execution_table row 2 under 'Resulting Extract Type' column.
At which step does the Extract type become "orange"?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
See execution_table row 3 where "orange" is included.
If we change the Extract to Extract<Fruit, "banana" | "lemon">, what would the Extract type be?
A"lemon"
B"orange"
C"banana"
D"banana" | "lemon"
💡 Hint
Only members from Fruit assignable to "banana" | "lemon" are kept; "lemon" is not in Fruit.
Concept Snapshot
Extract<Type, Union>
- Takes two types
- Keeps members of Type assignable to Union
- Result is a new union type
- Useful to filter union members
- If no match, result is never
Full Transcript
The Extract type in TypeScript takes two types: a type to extract and a union type. It checks each member of the union and keeps only those assignable to the second type. For example, given Fruit as "apple" | "banana" | "orange" and Extract<Fruit, "orange" | "lemon">, it checks each Fruit member. "apple" and "banana" are not assignable to "orange" | "lemon", so they are excluded. "orange" is assignable, so it is included. The final Extract type is "orange". This process is shown step-by-step in the execution table and variable tracker. Common confusions include why some members are excluded and what happens if no members match. Extract is useful to filter union types based on assignability.