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>?✗ Incorrect
Extract keeps only types from the first union assignable to the second. Only
number fits.If
T has no types assignable to U, what does Extract<T, U> return?✗ Incorrect
When no types match,
Extract returns never, meaning no possible value.Which utility type is the opposite of
Extract<T, U>?✗ Incorrect
Exclude<T, U> removes types assignable to U from T, opposite of Extract.What does
Extract<'a' | 'b' | 'c', 'b' | 'd'> evaluate to?✗ Incorrect
Only 'b' is common and assignable to both unions.
Can
Extract<T, U> be used with non-union types?✗ Incorrect
Extract works with any types, but is most useful with unions to filter types.
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.