Skip to content

Overloads

Function overloads in TypeScript allow you to define multiple function signatures for a single function name, enabling you to define functions that can be called in multiple ways. Here’s an example:

// Overloads
function sayHi(name: string): string;
function sayHi(names: string[]): string[];
// Implementation
function sayHi(name: unknown): unknown {
if (typeof name === 'string') {
return `Hi, ${name}!`;
} else if (Array.isArray(name)) {
return name.map(name => `Hi, ${name}!`);
}
throw new Error('Invalid value');
}
sayHi('xx'); // Valid
sayHi(['aa', 'bb']); // Valid

Here’s another example of using function overloads within a class:

class Greeter {
message: string;
constructor(message: string) {
this.message = message;
}
// overload
sayHi(name: string): string;
sayHi(names: string[]): ReadonlyArray<string>;
// implementation
sayHi(name: unknown): unknown {
if (typeof name === 'string') {
return `${this.message}, ${name}!`;
} else if (Array.isArray(name)) {
return name.map(name => `${this.message}, ${name}!`);
}
throw new Error('value is invalid');
}
}
console.log(new Greeter('Hello').sayHi('Simon'));