0
0
Typescriptprogramming~5 mins

Extract type in Typescript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the Extract<T, U> utility type do in TypeScript?
It creates a new type by selecting from type T only those types that are assignable to type U. Think of it as filtering T to keep only the parts that fit U.
Click to reveal answer
beginner
How would you use Extract<string | number | boolean, number | boolean>?
This extracts the types from string | number | boolean that are assignable to number | boolean. The result is number | boolean.
Click to reveal answer
intermediate
Why is Extract<T, U> useful in real code?
It helps you narrow down types when you have a union and want to keep only certain parts. For example, when filtering event types or extracting specific keys from a union of string literals.
Click to reveal answer
intermediate
What happens if no types in T are assignable to U in Extract<T, U>?
The result is never, meaning no types match the condition.
Click to reveal answer
beginner
Show a simple example of Extract<T, U> with string literal types.
Example: <br>type T = 'a' | 'b' | 'c';<br>type U = 'a' | 'c' | 'd';<br>type Result = Extract<T, U>; // 'a' | 'c'<br>This keeps only the common string literals between T and U.
Click to reveal answer
What is the result of Extract<string | number | boolean, number>?
Anumber
Bstring | number | boolean
Cstring
Dboolean
If T has no types assignable to U, what does Extract<T, U> return?
Anever
Bunknown
Cany
Dvoid
Which utility type is the opposite of Extract<T, U>?
AOmit&lt;T, U&gt;
BPick&lt;T, U&gt;
CExclude&lt;T, U&gt;
DPartial&lt;T&gt;
What does Extract<'a' | 'b' | 'c', 'b' | 'd'> evaluate to?
Anever
B'a' | 'b' | 'c' | 'd'
C'd'
D'b'
Can Extract<T, U> be used with non-union types?
ANo, it causes a compile error
BYes, it works the same way
CYes, but it always returns never
DNo, it only works with unions
Explain in your own words what the Extract<T, U> type does and give a simple example.
Think about keeping only the parts of T that fit inside U.
You got /3 concepts.
    How can Extract<T, U> help when working with union types in TypeScript?
    Consider when you want to keep only some options from a bigger set.
    You got /3 concepts.