A union type means a value can be one of several types. It uses the | symbol. For example, string | number means the value can be a string or a number.
Click to reveal answer
beginner
What is an intersection type in TypeScript?
An intersection type means a value must have all the types combined. It uses the & symbol. For example, {a: number} & {b: string} means the value has both properties a and b.
Click to reveal answer
beginner
How can you imagine a union type in real life?
Think of a union type like a menu where you can choose one item from several options. For example, choosing a drink: coffee or tea or juice.
Click to reveal answer
beginner
How can you imagine an intersection type in real life?
Think of an intersection type like a combo meal that includes all parts together. For example, a meal with a burger AND fries AND a drink.
Click to reveal answer
intermediate
What happens if you try to assign a value that fits neither union nor intersection types?
For a union type, the value must match at least one type. For an intersection type, the value must satisfy all types. If it doesn't, TypeScript will show an error.
Click to reveal answer
Which symbol represents a union type in TypeScript?
A*
B|
C+
D&
✗ Incorrect
The union type uses the pipe symbol | to mean 'or'.
Which symbol represents an intersection type in TypeScript?
A+
B|
C&
D*
✗ Incorrect
The intersection type uses the ampersand symbol & to mean 'and'.
If a variable is typed as string | number, which value is allowed?
A42
B{name: 'Alice'}
Ctrue
D[1, 2, 3]
✗ Incorrect
The value 42 is a number, which fits the union type string | number.
If a variable is typed as {a: number} & {b: string}, which object is valid?
A{}
B{b: 'hello'}
C{a: 5}
D{a: 5, b: 'hello'}
✗ Incorrect
The object must have both properties a and b to satisfy the intersection type.
Which mental model best describes a union type?
AA menu where you pick one item
BA combo meal with all items included
CA list of unrelated items
DA single fixed item
✗ Incorrect
A union type is like choosing one item from a menu of options.
Explain the difference between union and intersection types using a simple real-life example.
Think about choosing one item versus having all items together.
You got /3 concepts.
Describe how TypeScript uses the symbols | and & in types and what they mean.
Focus on the meaning of 'or' and 'and' in types.
You got /4 concepts.
Practice
(1/5)
1. Which statement best describes a union type in TypeScript?
easy
A. A value must have all combined types, like string & number.
B. A value can be one type or another, like string | number.
C. It restricts a value to only one specific type.
D. It creates a new type by excluding some properties.
Solution
Step 1: Understand union type meaning
A union type means a value can be one of several types, for example, string | number means the value can be either a string or a number.
Step 2: Compare with intersection type
An intersection type requires a value to have all combined types, which is different from union. So a value can be one type or another, like string | number, correctly describes union.
Final Answer:
A value can be one type or another, like string | number. -> Option B
Quick Check:
Union = one of multiple types [OK]
Hint: Union means one type OR another, intersection means ALL types [OK]
Common Mistakes:
Confusing union with intersection
Thinking union requires all types simultaneously
Mixing up syntax symbols | and &
2. Which of the following is the correct syntax for an intersection type combining A and B?
easy
A. type C = A | B;
B. type C = A, B;
C. type C = A + B;
D. type C = A & B;
Solution
Step 1: Recall intersection syntax
Intersection types use the ampersand & symbol to combine types, meaning the value must satisfy both types.
Step 2: Identify correct syntax
type C = A & B; uses A & B, which is the correct syntax for intersection types in TypeScript.
Final Answer:
type C = A & B; -> Option D
Quick Check:
Intersection uses & symbol [OK]
Hint: Intersection uses & symbol, union uses | symbol [OK]
Common Mistakes:
Using | instead of & for intersection
Using invalid operators like + or comma
Confusing type alias syntax
3. What is the type of variable value after this assignment?
type A = { name: string };
type B = { age: number };
let value: A | B = { name: "Alice", age: 30 };
medium
A. Error: value must be either A or B, not both.
B. Valid: value must have only name property.
C. Valid: value has both name and age properties.
D. Error: value must have only age property.
Solution
Step 1: Understand union type assignment
The variable value is declared as A | B, meaning it can be either type A or type B.
Step 2: Check assigned object
The assigned object has both name and age properties, which does not strictly satisfy either A or B alone, so it is not valid as a union type.
Final Answer:
Error: value must be either A or B, not both. -> Option A
Quick Check:
Union requires value to match one type exactly [OK]
Hint: Union requires value to match one type exactly [OK]
Common Mistakes:
Thinking union allows objects with all properties combined
Confusing union with intersection requirements
Assuming extra properties are allowed in union
4. Identify the error in this TypeScript code:
type A = { name: string };
type B = { age: number };
let person: A & B = { name: "Bob" };
medium
A. Syntax error in type declaration.
B. Incorrect use of union instead of intersection.
C. Missing property age in the object assigned to person.
D. Extra property name not allowed.
Solution
Step 1: Understand intersection type requirements
An intersection type A & B requires the value to have all properties from both types A and B.
Step 2: Check assigned object properties
The object assigned has only name property but is missing age, so it does not satisfy the intersection type.
Final Answer:
Missing property age in the object assigned to person. -> Option C
Quick Check:
Intersection requires all properties present [OK]
Hint: Intersection needs all properties present [OK]
Common Mistakes:
Thinking intersection allows partial properties
Confusing union and intersection requirements
Ignoring missing properties error
5. Given these types:
type Name = { name: string };
type Age = { age: number };
type Contact = { email: string };
// Which type correctly represents a value that must have a name and either age or email?
// Choose the correct type for Person:
hard
A. type Person = Name & (Age | Contact);
B. type Person = (Name & Age) | Contact;
C. type Person = Name | Age & Contact;
D. type Person = Name & Age & Contact;
Solution
Step 1: Analyze requirement
The value must have a name property and either an age or email property.
Step 2: Translate to types
This means the type must be Name AND (either Age OR Contact), which is Name & (Age | Contact).
Step 3: Check options
type Person = Name & (Age | Contact); matches this exactly. Other options either change the logic or require all three properties.
Final Answer:
type Person = Name & (Age | Contact); -> Option A
Quick Check:
Name AND (Age OR Contact) = Name & (Age | Contact) [OK]
Hint: Use parentheses to combine union and intersection correctly [OK]
Common Mistakes:
Ignoring parentheses changing logic
Mixing order of union and intersection
Choosing all properties instead of either/or
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.