0
0
Typescriptprogramming~20 mins

Declaring modules in Typescript - Mini Project: Build & Apply

Choose your learning style9 modes available
Declaring Modules in TypeScript
📖 Scenario: You are building a small TypeScript project that uses a custom module to organize your code. Modules help keep your code clean and reusable, just like separate boxes for your toys.
🎯 Goal: Create a module named mathUtils that exports a function to add two numbers. Then, import and use this function in your main code.
📋 What You'll Learn
Create a module named mathUtils with an exported function add that takes two numbers and returns their sum.
Import the add function from the mathUtils module.
Use the imported add function to add two numbers and store the result in a variable named result.
Print the value of result.
💡 Why This Matters
🌍 Real World
Modules help organize code in real projects, making it easier to maintain and reuse code across different files.
💼 Career
Understanding how to declare and use modules is essential for working on professional TypeScript projects and collaborating with other developers.
Progress0 / 4 steps
1
Create the mathUtils module with an add function
Create a module named mathUtils by writing export function add(a: number, b: number): number that returns the sum of a and b.
Typescript
Need a hint?

Use export function add(a: number, b: number): number and return a + b.

2
Import the add function from mathUtils
Write an import statement to import the add function from the mathUtils module.
Typescript
Need a hint?

Use import { add } from './mathUtils'; to import the function.

3
Use the add function to add two numbers
Call the imported 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 sum.

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

Use console.log(result); to display the sum.