Constructor Overloads
What you'll learn: How to define multiple constructor signatures so a class can be instantiated in different ways while maintaining full type safety.
The Problem
Just like functions, constructors sometimes need to accept different combinations of parameters. You might want to create an instance with all details, or with partial data, or by copying from another object — all through the same class.
How Constructor Overloads Work
Constructor overloads let you define multiple signatures (the public API) above a single implementation (the actual code). TypeScript checks calls against the signatures, but only the implementation runs:
class User {
name: string;
age: number;
// Signature 1: name and age
constructor(name: string, age: number);
// Signature 2: copy from another User
constructor(user: User);
// Implementation (must handle both cases)
constructor(nameOrUser: string | User, age?: number) {
if (typeof nameOrUser === 'string') {
this.name = nameOrUser;
this.age = age!;
} else {
this.name = nameOrUser.name;
this.age = nameOrUser.age;
}
}
}
const alice = new User('Alice', 30); // Uses signature 1
const bob = new User(alice); // Uses signature 2
Why This Matters
Constructor overloads support different initialization patterns without forcing you to use static factory methods or separate classes. TypeScript enforces that callers use one of the declared signatures, while your implementation handles all cases internally.
Critical Rule
The implementation signature must be compatible with all overload signatures. It won't appear in autocomplete — users only see the overloads.
Key Takeaway: Constructor overloads define multiple ways to instantiate a class with full type safety, keeping initialization logic centralized while supporting diverse creation patterns.