Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Defining Function Overloads | Function Overloading and Context
TypeScript Functions and Parameters

bookDefining 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.

1234567891011121314151617181920212223
function 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.
copy

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.

question mark

Which statements about TypeScript function overloading are correct?

Select the correct answer

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 3. Luku 1

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

Suggested prompts:

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

bookDefining Function Overloads

Pyyhkäise näyttääksesi valikon

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.

1234567891011121314151617181920212223
function 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.
copy

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.

question mark

Which statements about TypeScript function overloading are correct?

Select the correct answer

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 3. Luku 1
some-alt