Recall & Review
beginner
What is the basic syntax of a conditional type in TypeScript?
A conditional type uses the syntax: <br>
T extends U ? X : Y<br>It means if type T can be assigned to type U, then the type is X, otherwise it is Y.Click to reveal answer
beginner
Explain the role of
extends in conditional types.In conditional types,
extends checks if one type can be assigned to another. It acts like a question: "Does T fit into U?" If yes, the true branch is chosen; if no, the false branch is chosen.Click to reveal answer
beginner
What does this conditional type do?<br>
type IsString<T> = T extends string ? true : false;It checks if the type
T is a string. If yes, it returns true, otherwise false. For example, IsString<'hello'> = true and IsString<42> = false.Click to reveal answer
intermediate
Can conditional types be nested in TypeScript?
Yes, conditional types can be nested inside each other to create more complex type logic. For example:<br>
type Nested<T> = T extends string ? (T extends 'hello' ? true : false) : false;Click to reveal answer
intermediate
What happens if the condition in a conditional type is distributive?
If the type
T is a union, the conditional type is applied to each member separately, then results are combined. This is called distributive conditional types.Click to reveal answer
What does this conditional type return?<br>
type Check<T> = T extends number ? 'Yes' : 'No';<br>What is Check<string>?✗ Incorrect
Since
string does not extend number, the false branch 'No' is returned.In
T extends U ? X : Y, what happens if T is a union type?✗ Incorrect
Conditional types distribute over unions, applying the condition to each member individually.
What keyword is used to check type compatibility in conditional types?
✗ Incorrect
The keyword
extends is used to check if one type can be assigned to another.Which of these is a valid conditional type?
✗ Incorrect
The correct syntax uses
extends with the ternary operator: T extends U ? X : Y.What is the result of this type?<br>
type Result = 'a' | 'b' extends string ? true : false;✗ Incorrect
The union 'a' | 'b' extends string, so the condition is true.
Explain how conditional types work in TypeScript and give a simple example.
Think of it like an if-else for types.
You got /3 concepts.
Describe what distributive conditional types are and why they are useful.
Consider how a condition applies to multiple types inside a union.
You got /3 concepts.