TypeScript Union and Intersection Types Quiz

TypeScript
0 Passed
0% acceptance

A 40-question TypeScript quiz covering union types, narrowing, discriminated unions, intersections, type composition, compatibility, and real-world modelling.

40 Questions
~80 minutes
1

Question 1

What does a union type represent?

A
A value that may be one of several types
B
A merged object containing all fields
C
A runtime enum
D
A collection with fixed index types
2

Question 2

What values are allowed for x?

typescript
let x: number | string;
A
number or string
B
Only number
C
Only string
D
Any type
3

Question 3

Which operator helps narrow union types?

A
typeof
B
delete
C
instanceof only for arrays
D
new
4

Question 4

What does the check narrow value to inside the if block?

typescript
function f(v: number | string) {
  if (typeof v === 'string') {
    return v.length;
  }
}
A
string
B
number
C
boolean
D
never
5

Question 5

What is a discriminated union?

A
A union where each member has a shared literal field
B
A union that merges fields
C
A tuple-based union
D
A union only containing objects
6

Question 6

What does an intersection type represent?

A
A type that combines all fields of its components
B
A union of many types
C
A tuple of multiple elements
D
A runtime-merged instance
7

Question 7

What fields does Combined require?

typescript
type A = { x: number }
type B = { y: string }
type Combined = A & B;
A
x and y
B
x only
C
y only
D
None
8

Question 8

What happens if intersected types contain incompatible fields?

A
The resulting type becomes never
B
TypeScript auto-merges them
C
Union is produced instead
D
TypeScript ignores the conflict
9

Question 9

What is the resulting type?

typescript
type T = { a: string } & { a: number };
A
never
B
{ a: string }
C
{ a: number }
D
{ a: string | number }
10

Question 10

Which technique is commonly used to narrow unions of objects?

A
in-operator checks
B
abstract classes
C
tuple spreading
D
void expressions
11

Question 11

What does the check narrow v to?

typescript
type Cat = { meow: () => void }
type Dog = { bark: () => void }
function act(v: Cat | Dog) {
  if ('meow' in v) return 'cat';
}
A
Cat
B
Dog
C
never
D
any
12

Question 12

Which narrowing method applies to class instances?

A
instanceof
B
typeof
C
delete
D
import
13

Question 13

How is v narrowed?

typescript
class A {}
class B {}
function test(v: A | B) {
  if (v instanceof A) return 'A';
}
A
It becomes A
B
It becomes B
C
It becomes never
D
It becomes unknown
14

Question 14

What property usually acts as a discriminant?

A
A shared literal field
B
A tuple index
C
A computed symbol
D
A readonly modifier
15

Question 15

What type does handle receive?

typescript
type Load = { type: 'loading' }
type Success = { type: 'success', data: string }
type Result = Load | Success;
A
A discriminated union
B
A tuple
C
A merged object
D
A primitive alias
16

Question 16

Which structure ensures safe switching across union members?

A
switch on the discriminant value
B
multiple constructors
C
tuple iteration
D
map creation
17

Question 17

Which describes a union of object types?

A
Objects may vary in allowed fields
B
Objects must include all fields
C
Objects cannot differ
D
Objects must be instances of classes
18

Question 18

Which describes an intersection of object types?

A
Objects must satisfy all members
B
Objects can choose one of the branches
C
Objects require no fields
D
Objects narrow to primitives
19

Question 19

What does the value require?

typescript
type U = { a: number } | { b: number };
type I = { a: number } & { b: number };
A
U allows either shape; I requires both
B
U requires both; I requires none
C
Both allow either shape
D
Both require both shapes
20

Question 20

Where are intersections most useful?

A
Combining behaviours or features
B
Allowing any type to pass
C
Replacing inheritance entirely
D
Removing required fields
21

Question 21

What can this function accept?

typescript
function id(v: number | boolean) { return v }
A
number or boolean
B
string
C
object
D
tuple only
22

Question 22

Why might unions be used in API inputs?

A
To accept multiple data formats
B
To bypass type checking
C
To force runtime casting
D
To remove narrowability
23

Question 23

Which narrowing method helps detect nullable unions?

A
truthy checks
B
class constructors
C
namespace merging
D
getter methods
24

Question 24

What does the check guarantee?

typescript
function len(v: string | undefined) {
  if (v) return v.length;
}
A
v is a string inside the block
B
v is undefined
C
v is a number
D
v is a tuple
25

Question 25

Intersections are commonly used to:

A
Combine reusable behaviour constraints
B
Accept any type
C
Reduce type accuracy
D
Remove optional fields
26

Question 26

What type is returned?

typescript
type A = { a: number }
type B = { b: string }
function mix(): A & B { return { a: 1, b: 'x' } }
A
A combined type containing a and b
B
A union
C
A tuple
D
An empty object
27

Question 27

Why must intersected objects contain all fields?

A
Because each constituent type’s contract must be fulfilled
B
Because intersections widen types
C
Because TypeScript ignores missing fields
D
Because intersections convert types to unions
28

Question 28

Which scenario is ideal for intersection types?

A
Creating a type that has multiple capabilities
B
Allowing unknown values
C
Structuring a finite set of variant shapes
D
Representing single-choice options
29

Question 29

Which describes a union of literals?

A
A limited set of allowed literal values
B
A merged object
C
A tuple
D
A function-only type
30

Question 30

What is allowed for mode?

typescript
type Mode = 'on' | 'off';
let mode: Mode;
A
'on' or 'off'
B
true or false
C
any string
D
undefined only
31

Question 31

What issue can arise when widening a union?

A
Loss of specific literal types
B
Runtime errors from narrowing
C
Forced intersections
D
Structural mismatch
32

Question 32

What does an exhaustive switch on a discriminated union ensure?

A
All variants are handled
B
Only runtime checks occur
C
Intersections are created
D
Literal values are widened
33

Question 33

Which describes assignment compatibility in unions?

A
Assignments must match one branch
B
Assignments must match all branches
C
Assignments ignore branch rules
D
Assignments always fail
34

Question 34

What describes the resulting type of A & B & C?

A
All fields from A, B, and C
B
Any one of A, B, or C
C
A runtime tuple
D
An impossible type always
35

Question 35

Why must caution be used with deep intersections?

A
Conflicting nested fields may cause impossibility
B
They disallow objects
C
They behave like unions
D
They skip narrowing
36

Question 36

What is a common use case for unions?

A
Representing variant object shapes
B
Combining behaviours
C
Merging global types
D
Structure duplication
37

Question 37

Which narrowing technique works for both primitives and objects?

A
in-operator
B
boolean literal widening
C
globalThis access
D
readonly assertions
38

Question 38

How does TypeScript treat a function returning number | string?

A
The caller must handle both possibilities
B
It always returns a number
C
It always returns a string
D
It returns a tuple
39

Question 39

Why do unions sometimes require narrowing before property access?

A
Not all members guarantee the same properties
B
Unions forbid runtime access
C
Unions automatically erase properties
D
Narrowing is not allowed
40

Question 40

Which advantage do intersections offer over inheritance?

A
They allow combining multiple types without class chains
B
They prevent object creation
C
They automatically validate runtime shapes
D
They create deep readonly structures

QUIZZES IN TypeScript