TypeScript Union and Intersection Types Quiz
A 40-question TypeScript quiz covering union types, narrowing, discriminated unions, intersections, type composition, compatibility, and real-world modelling.
Question 1
What does a union type represent?
Question 2
What values are allowed for x?
let x: number | string;Question 3
Which operator helps narrow union types?
Question 4
What does the check narrow value to inside the if block?
function f(v: number | string) {
if (typeof v === 'string') {
return v.length;
}
}Question 5
What is a discriminated union?
Question 6
What does an intersection type represent?
Question 7
What fields does Combined require?
type A = { x: number }
type B = { y: string }
type Combined = A & B;Question 8
What happens if intersected types contain incompatible fields?
Question 9
What is the resulting type?
type T = { a: string } & { a: number };Question 10
Which technique is commonly used to narrow unions of objects?
Question 11
What does the check narrow v to?
type Cat = { meow: () => void }
type Dog = { bark: () => void }
function act(v: Cat | Dog) {
if ('meow' in v) return 'cat';
}Question 12
Which narrowing method applies to class instances?
Question 13
How is v narrowed?
class A {}
class B {}
function test(v: A | B) {
if (v instanceof A) return 'A';
}Question 14
What property usually acts as a discriminant?
Question 15
What type does handle receive?
type Load = { type: 'loading' }
type Success = { type: 'success', data: string }
type Result = Load | Success;Question 16
Which structure ensures safe switching across union members?
Question 17
Which describes a union of object types?
Question 18
Which describes an intersection of object types?
Question 19
What does the value require?
type U = { a: number } | { b: number };
type I = { a: number } & { b: number };Question 20
Where are intersections most useful?
Question 21
What can this function accept?
function id(v: number | boolean) { return v }Question 22
Why might unions be used in API inputs?
Question 23
Which narrowing method helps detect nullable unions?
Question 24
What does the check guarantee?
function len(v: string | undefined) {
if (v) return v.length;
}Question 25
Intersections are commonly used to:
Question 26
What type is returned?
type A = { a: number }
type B = { b: string }
function mix(): A & B { return { a: 1, b: 'x' } }Question 27
Why must intersected objects contain all fields?
Question 28
Which scenario is ideal for intersection types?
Question 29
Which describes a union of literals?
Question 30
What is allowed for mode?
type Mode = 'on' | 'off';
let mode: Mode;Question 31
What issue can arise when widening a union?
Question 32
What does an exhaustive switch on a discriminated union ensure?
Question 33
Which describes assignment compatibility in unions?
Question 34
What describes the resulting type of A & B & C?
Question 35
Why must caution be used with deep intersections?
Question 36
What is a common use case for unions?
Question 37
Which narrowing technique works for both primitives and objects?
Question 38
How does TypeScript treat a function returning number | string?
Question 39
Why do unions sometimes require narrowing before property access?
Question 40
Which advantage do intersections offer over inheritance?
