📖 Scenario: Imagine you are building a simple calculator app. You want to make sure the functions you write only accept numbers and return numbers. This helps avoid mistakes and makes your code safer and easier to understand.
🎯 Goal: You will create typed functions in TypeScript that add and multiply numbers. You will see how specifying types helps catch errors early and makes your code clearer.
📋 What You'll Learn
Create a function called add that takes two numbers and returns a number
Create a function called multiply that takes two numbers and returns a number
Call both functions with numbers and store the results
Print the results to the console
💡 Why This Matters
🌍 Real World
Typed functions are used in real apps to avoid bugs and make code easier to maintain.
💼 Career
Many companies use TypeScript to write safer JavaScript code, so knowing typed functions is important for developers.
Progress0 / 4 steps
1
Create the add function with typed parameters and return
Write a function called add that takes two parameters named a and b, both of type number, and returns a number. The function should return the sum of a and b.
Typescript
Hint
Use function add(a: number, b: number): number to define the function with typed parameters and return type.
2
Create the multiply function with typed parameters and return
Add a function called multiply that takes two parameters named x and y, both of type number, and returns a number. The function should return the product of x and y.
Typescript
Hint
Define multiply similar to add, but return the product of x and y.
3
Call the functions and store the results
Create two variables called sumResult and productResult. Assign sumResult the result of calling add with 5 and 3. Assign productResult the result of calling multiply with 5 and 3.
Typescript
Hint
Call add(5, 3) and multiply(5, 3) and store results in variables.
4
Print the results to the console
Write two console.log statements to print sumResult and productResult.
Typescript
Hint
Use console.log(sumResult) and console.log(productResult) to show the results.
Practice
(1/5)
1. Why is it important to specify types for function parameters and return values in TypeScript?
easy
A. It helps catch errors early by ensuring correct input and output types.
B. It makes the code run faster at runtime.
C. It allows functions to run without any input.
D. It automatically fixes bugs in the code.
Solution
Step 1: Understand the role of typed functions
Typed functions define what types of inputs they accept and what type they return, helping the developer know how to use them correctly.
Step 2: Recognize the benefit of early error detection
By specifying types, TypeScript can catch mistakes before running the code, preventing bugs caused by wrong data types.
Final Answer:
It helps catch errors early by ensuring correct input and output types. -> Option A
Quick Check:
Typed functions = early error detection [OK]
Hint: Typed functions catch type mistakes before running code [OK]
Common Mistakes:
Thinking types speed up runtime
Believing typed functions fix bugs automatically
Assuming functions don't need inputs
2. Which of the following is the correct way to type a function in TypeScript that takes a number and returns a string?
easy
A. function convert(num: number) { return num.toString(); }
B. function convert(num: number): string { return num.toString(); }
C. function convert(num: string): number { return num.length; }
D. function convert(num): string { return num.toString(); }
Solution
Step 1: Check parameter and return types
The function should accept a number parameter and return a string, so the parameter must be typed as number and the return type as string.
Step 2: Identify the correct syntax
function convert(num: number): string { return num.toString(); } correctly types the parameter as number and the return type as string. function convert(num): string { return num.toString(); } misses parameter type, C swaps types, and A misses return type.
Final Answer:
function convert(num: number): string { return num.toString(); } -> Option B
B. Change function parameter to number: greet(name: number).
C. No error, code runs fine.
D. Change greet(42) to greet('42') to match string parameter.
Solution
Step 1: Check function parameter type
The function expects a string parameter named name.
Step 2: Identify the call with wrong argument type
The call greet(42) passes a number, which conflicts with the string type.
Step 3: Fix the call to match expected type
Changing greet(42) to greet('42') passes a string, fixing the type mismatch.
Final Answer:
Change greet(42) to greet('42') to match string parameter. -> Option D
Quick Check:
Parameter type mismatch fixed by passing string [OK]
Hint: Match argument types to parameter types exactly [OK]
Common Mistakes:
Ignoring type mismatch errors
Removing return type instead of fixing input
Changing parameter type without reason
5. You want a function that accepts either a string or number and returns a string describing the input type. Which typed function is correct?
hard
A. function describe(input: any): string {
return `Input: ${input}`;
}
B. function describe(input: string): string {
return `String: ${input}`;
}
C. function describe(input: string | number): string {
if (typeof input === 'string') return `String: ${input}`;
else return `Number: ${input}`;
}
D. function describe(input: number): string {
return `Number: ${input}`;
}
Solution
Step 1: Understand the input types needed
The function must accept either string or number, so union type string | number is required.
Step 2: Check function logic and typing
function describe(input: string | number): string {
if (typeof input === 'string') return `String: ${input}`;
else return `Number: ${input}`;
} uses union type and checks input type to return correct description. Options C and D accept only one type, A uses any which loses type safety.
Final Answer:
function describe(input: string | number): string {
if (typeof input === 'string') return `String: ${input}`;
else return `Number: ${input}`;
} -> Option C
Quick Check:
Union type with type check = function describe(input: string | number): string {
if (typeof input === 'string') return `String: ${input}`;
else return `Number: ${input}`;
} [OK]
Hint: Use union types and type checks for multiple input types [OK]
Common Mistakes:
Using any type losing safety
Accepting only one type instead of both
Not checking input type before returning
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.