Bird
Raised Fist0
Typescriptprogramming~20 mins

Union vs intersection mental model in Typescript - Practice Questions

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Union vs Intersection Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of union type variable assignment
What will be the output of the following TypeScript code?
Typescript
type A = { name: string } | { age: number };

const person: A = { name: "Alice", age: 30 };
console.log(person);
A{ name: "Alice", age: 30 }
BTypeScript error: Object literal may only specify known properties
C{ name: "Alice" }
D{ age: 30 }
Attempts:
2 left
💡 Hint
For union types of object shapes, object literals must match exactly one member without excess properties due to TypeScript's excess property checking.
❓ Predict Output
intermediate
2:00remaining
Output of intersection type variable assignment
What will be the output of this TypeScript code?
Typescript
type B = { name: string } & { age: number };

const person: B = { name: "Bob", age: 25 };
console.log(person);
A{ name: "Bob", age: 25 }
BTypeScript error: Type '{ name: string; age: number; }' is not assignable to type 'B'
C{ name: "Bob" }
D{ age: 25 }
Attempts:
2 left
💡 Hint
Intersection means the object must have all properties from both types.
🧠 Conceptual
advanced
1:30remaining
Understanding union vs intersection types
Which statement best describes the difference between union and intersection types in TypeScript?
AUnion types allow a value to be any one of the types, while intersection types require a value to satisfy all types simultaneously.
BUnion types and intersection types are the same and can be used interchangeably.
CUnion types require a value to satisfy all types simultaneously, while intersection types allow a value to satisfy any one of the types.
DUnion types only work with primitive types, while intersection types only work with objects.
Attempts:
2 left
💡 Hint
Think about whether the value needs to meet one or all conditions.
❓ Predict Output
advanced
2:30remaining
Output of function with union and intersection parameters
What is the output of this TypeScript code?
Typescript
type X = { a: number } | { b: string };

type Y = { a: number } & { b: string };

function testUnion(x: X) {
  if ('a' in x) return x.a;
  return x.b.length;
}

function testIntersection(y: Y) {
  return y.a + y.b.length;
}

console.log(testUnion({ a: 10 }));
console.log(testUnion({ b: "hello" }));
console.log(testIntersection({ a: 5, b: "world" }));
A
TypeScript error
TypeScript error
TypeScript error
B
01
5
01
C
10
5
TypeScript error
D
10
5
10
Attempts:
2 left
💡 Hint
Check how the functions handle union and intersection types differently.
🔧 Debug
expert
2:00remaining
Why does this intersection type assignment fail?
Why does the following TypeScript code produce an error?
Typescript
type C = { x: number } & { y: number };

const obj: C = { x: 1 };
ABecause intersection types cannot be assigned to variables.
BBecause obj has an extra property 'x' not allowed in intersection types.
CBecause obj is missing property 'y' required by the intersection type.
DBecause TypeScript does not support intersection types with objects.
Attempts:
2 left
💡 Hint
Think about what intersection means for required properties.

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

  1. 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.
  2. 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.
  3. Final Answer:

    A value can be one type or another, like string | number. -> Option B
  4. 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

  1. Step 1: Recall intersection syntax

    Intersection types use the ampersand & symbol to combine types, meaning the value must satisfy both types.
  2. Step 2: Identify correct syntax

    type C = A & B; uses A & B, which is the correct syntax for intersection types in TypeScript.
  3. Final Answer:

    type C = A & B; -> Option D
  4. 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

  1. Step 1: Understand union type assignment

    The variable value is declared as A | B, meaning it can be either type A or type B.
  2. 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.
  3. Final Answer:

    Error: value must be either A or B, not both. -> Option A
  4. 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

  1. Step 1: Understand intersection type requirements

    An intersection type A & B requires the value to have all properties from both types A and B.
  2. 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.
  3. Final Answer:

    Missing property age in the object assigned to person. -> Option C
  4. 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

  1. Step 1: Analyze requirement

    The value must have a name property and either an age or email property.
  2. Step 2: Translate to types

    This means the type must be Name AND (either Age OR Contact), which is Name & (Age | Contact).
  3. Step 3: Check options

    type Person = Name & (Age | Contact); matches this exactly. Other options either change the logic or require all three properties.
  4. Final Answer:

    type Person = Name & (Age | Contact); -> Option A
  5. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard