Recall & Review
beginner
What is a conditional type in TypeScript?
A conditional type chooses one type or another based on a condition, like an if-else for types.
Click to reveal answer
beginner
Why do we need conditional types in TypeScript?
They let us create flexible types that change based on input types, making code safer and easier to reuse.Click to reveal answer
intermediate
How do conditional types improve code safety?
By selecting types based on conditions, they prevent errors by ensuring variables have the right type in different situations.
Click to reveal answer
beginner
Give a simple example of a conditional type.
Example: `type IsString<T> = T extends string ? true : false;` This checks if T is a string and returns true or false.
Click to reveal answer
intermediate
What problem do conditional types solve that regular types can't?
They solve the problem of needing types that adapt based on other types, which normal static types can't do.
Click to reveal answer
What does a conditional type in TypeScript do?
✗ Incorrect
Conditional types choose one type or another depending on a condition.
Which syntax represents a conditional type?
✗ Incorrect
The syntax 'T extends U ? X : Y' is how conditional types are written.
Why are conditional types useful?
✗ Incorrect
Conditional types let types adapt depending on other types, increasing flexibility.
What will this conditional type return? `type Check<T> = T extends number ? 'Yes' : 'No';` if T is string?
✗ Incorrect
Since string does not extend number, the type returns 'No'.
Can conditional types help reduce code duplication?
✗ Incorrect
Conditional types let you write one type that works for many cases, reducing repeated code.
Explain in your own words why conditional types are needed in TypeScript.
Think about how types can change depending on other types.
You got /4 concepts.
Describe a simple example where a conditional type improves your code.
Try to imagine checking if a type is string or number.
You got /4 concepts.