Recall & Review
beginner
What is a union type in TypeScript?
A union type allows a variable to hold values of different types, like a choice between multiple types.
Click to reveal answer
beginner
Why do we need union types instead of just one type?
Because sometimes a value can be more than one type, like a number or a string, and union types let us handle that safely.Click to reveal answer
intermediate
How do union types help prevent errors?
They make sure you only use the allowed types and force you to check which type you have before using it, avoiding mistakes.
Click to reveal answer
beginner
Give an example of a union type in TypeScript.
Example: let value: number | string; means value can be a number or a string.Click to reveal answer
beginner
What real-life situation is similar to union types?
Like a key that can open either your house or your car. You have one key but it works for two things, just like one variable can hold two types.
Click to reveal answer
What does a union type allow in TypeScript?
✗ Incorrect
Union types let variables hold values of more than one type.
Which symbol is used to define a union type?
✗ Incorrect
The pipe symbol | is used to separate types in a union.
Why should you check the type of a union variable before using it?
✗ Incorrect
Checking type ensures you use the variable correctly and avoid errors.
Which of these is a valid union type declaration?
✗ Incorrect
The correct syntax uses | to separate types.
What problem do union types solve?
✗ Incorrect
Union types let variables hold values of different types safely.
Explain why union types are useful in TypeScript with an example.
Think about variables that can hold more than one type.
You got /3 concepts.
Describe how union types help prevent errors in your code.
Consider what happens if you use a variable without knowing its type.
You got /3 concepts.