0
0
Typescriptprogramming~15 mins

Parameter type annotations in Typescript - Mini Project: Build & Apply

Choose your learning style9 modes available
Parameter Type Annotations in TypeScript
📖 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 with type annotations and returns their sum.
📋 What You'll Learn
Create a function named add with two parameters: a and b.
Add type annotations to both parameters to specify they are numbers.
Return the sum of a and b.
Call the add function with arguments 5 and 7 and print the result.
💡 Why This Matters
🌍 Real World
Type annotations are used in real projects to make code easier to understand and safer by preventing wrong types from being used.
💼 Career
Many programming jobs require writing TypeScript with proper type annotations to maintain large codebases and reduce bugs.
Progress0 / 4 steps
1
Create the add function with parameters
Write a function called add that takes two parameters named a and b without type annotations yet.
Typescript
Need a hint?

Start by writing a function with two parameters. Don't add types yet.

2
Add type annotations to parameters
Modify the add function to add type annotations : number to both parameters a and b.
Typescript
Need a hint?

Use : number after each parameter name to specify the type.

3
Call the add function with numbers
Call the add function with arguments 5 and 7 and store the result in a variable named result.
Typescript
Need a hint?

Use const result = add(5, 7); to call the function and save the output.

4
Print the result
Write a console.log statement to print the value of the variable result.
Typescript
Need a hint?

Use console.log(result); to show the sum in the console.