typescript / beginner
Snippet
Function Overloads
Function overloads allow you to define multiple ways a function can be called, providing better type checking for different input combinations.
snippet.ts
1
2
3
4
5
function combine(a: number, b: number): number;function combine(a: string, b: string): string;function combine(a: any, b: any): any {return a + b;}
Breakdown
1
function combine(a: number, b: number): number;
An overload signature defining that if two numbers are passed, a number is returned.
2
function combine(a: any, b: any): any { ... }
The actual implementation of the function that handles the logic for all signatures.