What if you could instantly grab just the pieces you want from a big puzzle without searching through every piece?
Why Extract type in Typescript? - Purpose & Use Cases
Imagine you have a big box of mixed toys, and you want to pick only the red cars out of it by hand every time you play.
Manually sorting through all toys each time is slow, tiring, and you might miss some red cars or pick wrong toys by mistake.
The Extract type lets you quickly and safely pick only the parts you want from a big set, like grabbing all red cars instantly without sorting by hand.
type Vehicle = 'car' | 'truck' | 'bike'; type RedVehicles = 'car';
type Vehicle = 'car' | 'truck' | 'bike'; type RedVehicles = Extract<Vehicle, 'car'>;
You can easily create new types by selecting exactly what you need from existing ones, making your code cleaner and safer.
When building a form, you might want to extract only the string fields from a big user data type to handle text inputs separately.
Extract type helps pick specific parts from a bigger type.
It saves time and avoids mistakes compared to manual selection.
It makes your TypeScript code clearer and more reliable.
Practice
Extract<T, U> utility type do?Solution
Step 1: Understand the purpose of Extract
Extract<T, U>picks fromTonly those types that can be assigned toU.Step 2: Compare with other utility types
UnlikeExclude<T, U>which removes types,Extractkeeps matching types.Final Answer:
It creates a new type by picking types fromTthat are assignable toU. -> Option CQuick Check:
Extract = picks matching types [OK]
- Confusing Extract with Exclude
- Thinking Extract combines types
- Assuming Extract converts types to strings
type U = string | number | boolean?Solution
Step 1: Identify the correct order of parameters
Extract<T, U>takes the big unionTfirst, then the type to pickU.Step 2: Match the desired extracted type
We want to pickstringfromU = string | number | boolean, soExtract<U, string>is correct.Final Answer:
type OnlyString = Extract<U, string>; -> Option AQuick Check:
Extract<Union, PickType> syntax [OK]
- Swapping the order of parameters
- Picking wrong type in second parameter
- Using Extract with unrelated types
type Mixed = string | number | boolean;
type Result = Extract;
What is the type of
Result?Solution
Step 1: Understand the union types involved
Mixedisstring | number | boolean. We want to extract types assignable tonumber | boolean.Step 2: Extract matching types
FromMixed, onlynumberandbooleanmatchnumber | boolean.stringis excluded.Final Answer:
number | boolean -> Option DQuick Check:
Extract picks matching types [OK]
- Including types not in second parameter
- Confusing Extract with union
- Thinking Extract returns never if partial match
type A = string | number;
type B = Extract;
What will be the type of
B?Solution
Step 1: Analyze the Extract parameters
Aisstring | number, and we try to extractbooleantypes.Step 2: Determine matching types
Sincebooleanis not assignable tostringornumber, no types match, so the result isnever.Final Answer:
never -> Option BQuick Check:
No matching types means never [OK]
- Expecting an error instead of never
- Assuming Extract returns original union
- Confusing Extract with Exclude
type Data = string | number | null | undefined. You want to create a type that excludes null and undefined but only keeps string and number. Which Extract usage achieves this?Solution
Step 1: Identify the types to keep
We want to keep onlystringandnumber, excludingnullandundefined.Step 2: Use Extract with correct second parameter
UsingExtract<Data, string | number>picks onlystringandnumberfromData.Final Answer:
type CleanData = Extract<Data, string | number>; -> Option AQuick Check:
Extract filters union by matching types [OK]
- Including null or undefined in second param
- Using Exclude instead of Extract
- Confusing Extract with union
Ready to prove it?
Pass the test and this pattern moves unknown
