📖 Scenario: You are building a simple calculator function that adds two numbers. To make your code clear and safe, you want to specify the types of the inputs.
🎯 Goal: Create a function called add that takes two numbers as parameters and returns their sum. Use type annotations on the function parameters.
📋 What You'll Learn
Create a function named add
Add type annotations to both parameters to specify they are numbers
Return the sum of the two parameters
💡 Why This Matters
🌍 Real World
Type annotations help catch mistakes early and make your code easier to understand, especially when working with others.
💼 Career
Many programming jobs require writing clear, typed code to reduce bugs and improve maintainability.
Progress0 / 4 steps
1
Create the function skeleton
Write a function called add that takes two parameters named a and b without any type annotations yet.
Typescript
Hint
Start by writing the function name and parameters without types. Then return the sum of a and b.
2
Add type annotations to parameters
Add type annotations to the parameters a and b to specify they are both of type number.
Typescript
Hint
Use : number after each parameter name to add the type annotation.
3
Add a variable to hold the result
Inside the function add, create a variable called result of type number that stores the sum of a and b. Then return result.
Typescript
Hint
Declare result with const and add the type : number. Then return it.
4
Test the function output
Write a console.log statement to call add(5, 7) and print the result.
Typescript
Hint
Use console.log(add(5, 7)) to see the sum printed.
Practice
(1/5)
1. What does the type annotation in the function parameter function greet(name: string) tell us?
easy
A. The function expects name to be a string.
B. The function will return a string.
C. The function does not accept any parameters.
D. The function expects name to be a number.
Solution
Step 1: Understand the syntax of type annotation
The colon : after name means we are telling TypeScript what type name should be.
Step 2: Identify the type specified
The type specified is string, so name must be a string.
Final Answer:
The function expects name to be a string. -> Option A
Quick Check:
Parameter type annotation = string [OK]
Hint: Look for colon after parameter name to find its type [OK]
Common Mistakes:
Confusing parameter type with return type
Ignoring the colon and thinking it's optional
Assuming no parameters are accepted
2. Which of the following is the correct way to add a type annotation for a number parameter age in a TypeScript function?
easy
A. function setAge(age = number) {}
B. function setAge(age: number) {}
C. function setAge(number age) {}
D. function setAge(age) : number {}
Solution
Step 1: Recall correct syntax for parameter type annotation
TypeScript uses a colon : after the parameter name to specify its type.
Step 2: Check each option
function setAge(age: number) {} uses age: number, which is correct. Others use invalid syntax.
Final Answer:
function setAge(age: number) {} -> Option B
Quick Check:
Parameter type syntax = colon after name [OK]
Hint: Use colon after parameter name for type [OK]
Common Mistakes:
Using equals sign instead of colon for type
Placing type before parameter name
Adding colon after parentheses instead of parameter
3. What will be the output of this TypeScript code?
function multiply(x: number, y: number): number {
return x * y;
}
console.log(multiply(3, 4));
medium
A. 34
B. Error: Missing return type
C. undefined
D. 12
Solution
Step 1: Understand function parameters and return
The function takes two numbers and returns their product.
Step 2: Calculate multiply(3, 4)
3 times 4 equals 12, so the function returns 12.
Final Answer:
12 -> Option D
Quick Check:
3 * 4 = 12 [OK]
Hint: Multiply parameters as numbers to get output [OK]
Common Mistakes:
Confusing string concatenation with multiplication
Ignoring return type annotation
Expecting error due to missing type
4. Identify the error in this TypeScript function:
function greet(name string) {
return `Hello, ${name}`;
}
medium
A. Incorrect use of backticks for string
B. Missing return type annotation
C. Missing colon between parameter name and type
D. Parameter name cannot be 'name'
Solution
Step 1: Check parameter syntax
The parameter name string is missing a colon : between name and string.
Step 2: Confirm other parts are correct
Backticks and parameter name are valid; return type annotation is optional.
Final Answer:
Missing colon between parameter name and type -> Option C
Quick Check:
Parameter type needs colon [OK]
Hint: Always put colon between parameter and type [OK]
Common Mistakes:
Omitting colon between parameter and type
Thinking return type is mandatory
Confusing string quotes with backticks
5. You want to write a function formatPrice that takes a number price and returns a string with a dollar sign. Which is the correct way to add type annotations to the parameter and return type?
hard
A. function formatPrice(price: number): string { return `$${price}`; }
B. function formatPrice(price): string { return `$${price}`; }
C. function formatPrice(price: string): number { return `$${price}`; }
D. function formatPrice(price: number) { return price; }
Solution
Step 1: Determine parameter type
The parameter price should be a number because it represents a price value.
Step 2: Determine return type
The function returns a string with a dollar sign, so return type is string.
Step 3: Check options
function formatPrice(price: number): string { return `$${price}`; } correctly annotates price: number and return type : string. Others have missing or wrong types.
Final Answer:
function formatPrice(price: number): string { return `$${price}`; } -> Option A
Quick Check:
Parameter number, return string [OK]
Hint: Parameter type matches input, return type matches output [OK]
Common Mistakes:
Omitting parameter type
Wrong return type annotation
Returning wrong data type
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.