Recall & Review
beginner
What does the
Exclude<T, U> type do in TypeScript?It creates a new type by removing from
T all types that are assignable to U. Think of it as filtering out unwanted types from a set.Click to reveal answer
beginner
Given
type A = 'a' | 'b' | 'c' and type B = 'a' | 'c', what is Exclude<A, B>?The result is
'b' because 'a' and 'c' are removed from A since they are in B.Click to reveal answer
intermediate
How can
Exclude help when working with union types?It helps you create a new type by excluding certain members from a union, making your types more precise and avoiding unwanted values.
Click to reveal answer
beginner
Is
Exclude<string | number, number> equivalent to string?Yes. It removes
number from the union, leaving only string.Click to reveal answer
intermediate
Can
Exclude be used with non-union types?Yes, but if
T is not a union, it either returns T if it is not assignable to U, or never if it is assignable.Click to reveal answer
What is the result of
Exclude<'x' | 'y' | 'z', 'y' | 'a'>?✗ Incorrect
The type removes 'y' from the first union. Since 'a' is not in the first union, only 'y' is removed, leaving 'x' and 'z'.
Which utility type removes types from a union?
✗ Incorrect
Exclude removes types from a union. The others serve different purposes.What does
Exclude<number | string, string> evaluate to?✗ Incorrect
It removes
string from the union, leaving only number.If
T is 'a' and U is 'a', what is Exclude<T, U>?✗ Incorrect
Since
T is assignable to U, it is excluded, resulting in never.Can
Exclude be used to exclude types other than string literals?✗ Incorrect
Exclude works with any types, including primitives, objects, and unions.Explain how the
Exclude type works in TypeScript and give a simple example.Think about filtering out unwanted types from a set.
You got /3 concepts.
Describe a practical scenario where using
Exclude would help improve your TypeScript code.Consider when you want to remove some options from a list of possible values.
You got /3 concepts.