📖 Scenario: You are building a simple calculator that can add numbers or concatenate strings. Depending on the input types, the calculator should behave differently.
🎯 Goal: Create a TypeScript function called combine that works with both numbers and strings using function overloads. It should add numbers or join strings.
📋 What You'll Learn
Create two function overload signatures for combine: one for two numbers, one for two strings
Implement the combine function to handle both cases
Test the function by calling combine with numbers and strings
Print the results of the function calls
💡 Why This Matters
🌍 Real World
Function overloads help create flexible functions that work with different types of inputs, like calculators or formatters.
💼 Career
Many programming jobs require writing clean, reusable code that handles multiple data types safely, especially in TypeScript.
Progress0 / 4 steps
1
Create function overload signatures
Write two function overload signatures for a function called combine. The first signature takes two number parameters and returns a number. The second signature takes two string parameters and returns a string.
Typescript
Hint
Function overloads are multiple function declarations with the same name but different parameter types.
2
Implement the combine function
Implement the combine function with parameters a and b of type any. Inside the function, check if both a and b are numbers and return their sum. Otherwise, return their concatenation as strings.
Typescript
Hint
Use typeof to check the types inside the function and return accordingly.
3
Call the combine function with numbers and strings
Call the combine function twice: once with numbers 5 and 10, and once with strings 'Hello' and 'World'. Store the results in variables called resultNumbers and resultStrings respectively.
Typescript
Hint
Call the function with exact values and store the results in variables.
4
Print the results
Print the values of resultNumbers and resultStrings using two separate console.log statements.
Typescript
Hint
Use console.log to show the results on the screen.
Practice
(1/5)
1. What is the main purpose of function overloads in TypeScript?
easy
A. To allow a single function to handle different types of input with clear signatures
B. To create multiple functions with the same name but different implementations
C. To automatically generate function documentation
D. To enforce that functions only accept one specific type
Solution
Step 1: Understand function overloads concept
Function overloads let one function have multiple type signatures to handle different input types clearly.
Step 2: Compare options with concept
Only To allow a single function to handle different types of input with clear signatures correctly describes this purpose. Other options describe different or incorrect ideas.
Final Answer:
To allow a single function to handle different types of input with clear signatures -> Option A
Quick Check:
Function overloads = multiple input types handled clearly [OK]
Hint: Overloads = multiple input types, one function name [OK]
Common Mistakes:
Thinking overloads create multiple separate functions
Believing overloads generate docs automatically
Assuming overloads restrict to one type only
2. Which of the following is the correct syntax for declaring function overloads in TypeScript?
easy
A. function foo(x: any): any; function foo(x: any) { return x; }
B. function foo(x: number | string): number | string { return x; }
C. function foo(x: number): number { } function foo(x: string): string { }
D. function foo(x: number): number; function foo(x: string): string; function foo(x: any) { return x; }
Solution
Step 1: Identify correct overload syntax
Overloads require multiple declarations without bodies, then one implementation with a body.
Step 2: Check each option
function foo(x: number): number; function foo(x: string): string; function foo(x: any) { return x; } shows multiple signatures without bodies, then one implementation with a body. Others either combine signatures or have bodies in all declarations.
Final Answer:
function foo(x: number): number; function foo(x: string): string; function foo(x: any) { return x; } -> Option D
Quick Check:
Overloads = multiple signatures + one implementation [OK]
Hint: Overloads: signatures no body, one implementation with body [OK]
Common Mistakes:
Putting function bodies in all overload declarations
Using union types instead of overloads
Declaring only one function signature
3. What will be the output of the following TypeScript code?
function combine(a: number, b: number): number;
function combine(a: string, b: string): string;
function combine(a: any, b: any) {
return a + b;
}
console.log(combine(5, 10));
console.log(combine('Hi, ', 'there!'));
medium
A. '510' and 'Hi, there!'
B. 15 and 'Hithere!'
C. 15 and 'Hi, there!'
D. Error: No matching overload
Solution
Step 1: Analyze overload signatures and implementation
Two overloads: one for numbers returning number, one for strings returning string. Implementation uses + operator.