Defining Function Overloads
Function overloading in TypeScript allows you to define multiple signatures for a single function, enabling it to handle different sets of parameter types and return types. This feature is especially useful when you want a function to behave differently based on the types of arguments provided, such as combining numbers or concatenating strings with the same function name. By declaring multiple overload signatures, you provide clear contracts for how your function can be called, making your code more flexible and expressive.
1234567891011121314151617181920212223function combine(a: string, b: string): string; function combine(a: number, b: number): number; // Implementation signature function combine(a: any, b: any): any { if (typeof a === "string" && typeof b === "string") { return a + b; // Concatenate strings } if (typeof a === "number" && typeof b === "number") { return a + b; // Add numbers } throw new Error("Invalid arguments"); } // Valid calls console.log(combine("Hello, ", "world")); // "Hello, world" console.log(combine(10, 20)); // 30 // Invalid (failure) calls // The following will cause compile-time errors in TypeScript: // console.log(combine("Hello", 5)); // Error: No overload matches this call. // console.log(combine(true, false)); // Error: No overload matches this call. // console.log(combine(1, "2")); // Error: No overload matches this call.
When you declare overloaded functions in TypeScript, you start by writing one or more overload signatures—these define the different ways your function can be called. The actual function implementation comes last, with a parameter list that can handle all possible argument types. TypeScript uses the overload signatures to check your calls at compile time, ensuring you only use supported parameter combinations. Inside the function body, you typically use type checks, such as typeof, to determine which logic to apply for the given arguments. This approach lets you write a single implementation that safely supports multiple input types, while providing type safety and clear documentation for anyone using your function.
Bedankt voor je feedback!
Vraag AI
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.
Can you explain how TypeScript chooses which overload signature to use?
What happens if I call the function with unsupported argument types?
Can you show more examples of function overloading in TypeScript?
Awesome!
Completion rate improved to 9.09
Defining Function Overloads
Veeg om het menu te tonen
Function overloading in TypeScript allows you to define multiple signatures for a single function, enabling it to handle different sets of parameter types and return types. This feature is especially useful when you want a function to behave differently based on the types of arguments provided, such as combining numbers or concatenating strings with the same function name. By declaring multiple overload signatures, you provide clear contracts for how your function can be called, making your code more flexible and expressive.
1234567891011121314151617181920212223function combine(a: string, b: string): string; function combine(a: number, b: number): number; // Implementation signature function combine(a: any, b: any): any { if (typeof a === "string" && typeof b === "string") { return a + b; // Concatenate strings } if (typeof a === "number" && typeof b === "number") { return a + b; // Add numbers } throw new Error("Invalid arguments"); } // Valid calls console.log(combine("Hello, ", "world")); // "Hello, world" console.log(combine(10, 20)); // 30 // Invalid (failure) calls // The following will cause compile-time errors in TypeScript: // console.log(combine("Hello", 5)); // Error: No overload matches this call. // console.log(combine(true, false)); // Error: No overload matches this call. // console.log(combine(1, "2")); // Error: No overload matches this call.
When you declare overloaded functions in TypeScript, you start by writing one or more overload signatures—these define the different ways your function can be called. The actual function implementation comes last, with a parameter list that can handle all possible argument types. TypeScript uses the overload signatures to check your calls at compile time, ensuring you only use supported parameter combinations. Inside the function body, you typically use type checks, such as typeof, to determine which logic to apply for the given arguments. This approach lets you write a single implementation that safely supports multiple input types, while providing type safety and clear documentation for anyone using your function.
Bedankt voor je feedback!