0
0
Typescriptprogramming~20 mins

Union vs intersection mental model in Typescript - Practice Questions

Choose your learning style9 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.