Skip to main content
Practice Problems

Intersection types in TypeScript

What are Intersection Types?

An intersection type combines multiple types into one using the & operator. The resulting type has all properties from all combined types.


Basic Syntax

typescript
type A = { name: string }; type B = { age: number }; type AB = A & B; // AB = { name: string; age: number } const person: AB = { name: "Alice", age: 25 }; // Must have BOTH name and age

Intersection vs Union

OperatorMeaningResult
A & B (Intersection)Must satisfy both A and BAll properties from both
A | B (Union)Must satisfy either A or BProperties common to both
typescript
type Dog = { bark(): void; legs: number }; type Bird = { fly(): void; legs: number }; type DogAndBird = Dog & Bird; // { bark(): void; fly(): void; legs: number } type DogOrBird = Dog | Bird; // Only 'legs' is guaranteed accessible

Combining Interfaces

typescript
interface HasId { id: string; } interface HasTimestamps { createdAt: Date; updatedAt: Date; } interface HasSoftDelete { deletedAt: Date | null; } type BaseEntity = HasId & HasTimestamps & HasSoftDelete; // Has all: id, createdAt, updatedAt, deletedAt

Practical Use Cases

Mixins / Composing Types

typescript
type Draggable = { drag(): void; position: { x: number; y: number }; }; type Resizable = { resize(w: number, h: number): void; size: { width: number; height: number }; }; type UIWidget = Draggable & Resizable;

Extending Function Parameters

typescript
type BaseConfig = { debug: boolean; verbose: boolean }; type DBConfig = BaseConfig & { connectionString: string; pool: number }; type CacheConfig = BaseConfig & { ttl: number; maxSize: number }; function initDB(config: DBConfig) { /* ... */ }

API Response Typing

typescript
type ApiResponse<T> = { status: number; message: string; } & T; type UserResponse = ApiResponse<{ data: { name: string; email: string } }>; // { status: number; message: string; data: { name: string; email: string } }

Conflicting Properties

When intersected types have the same property with incompatible types, the result is never:

typescript
type A = { value: string }; type B = { value: number }; type C = A & B; // value: string & number → never (impossible to satisfy) const c: C = { value: "hello" }; // ❌ Error

If the types are compatible, they narrow:

typescript
type A = { value: string | number }; type B = { value: number | boolean }; type C = A & B; // value: number (the overlap of string|number and number|boolean)

Important:

Intersection types are the TypeScript equivalent of "AND" — the resulting type must satisfy all combined types. Use them to compose complex types from simpler building blocks. Watch out for conflicting property types which result in never.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.

Finished reading?
Practice Problems