📖 Scenario: You are building a small TypeScript project with two modules. One module will export a function as a default export, and the other module will export multiple functions as named exports. You will learn how to create and use both default and named exports.
🎯 Goal: Create a TypeScript project with one module using a default export and another module using named exports. Then import and use these exports in a main file.
📋 What You'll Learn
Create a module file greetDefault.ts with a default exported function called greetDefault that returns the string 'Hello from default export!'.
Create a module file greetNamed.ts with two named exported functions called greetMorning and greetEvening that return 'Good morning!' and 'Good evening!' respectively.
Create a main file main.ts that imports the default export from greetDefault.ts and the named exports from greetNamed.ts.
Call all three functions in main.ts and print their returned strings.
💡 Why This Matters
🌍 Real World
Modules with default and named exports are common in TypeScript projects to organize code and share functionality between files.
💼 Career
Understanding how to use default and named exports is essential for working on modern TypeScript and JavaScript codebases in professional development.
Progress0 / 4 steps
1
Create default export function
Create a file called greetDefault.ts. Inside it, write a function called greetDefault that returns the string 'Hello from default export!'. Export this function as the default export using export default greetDefault.
Typescript
Hint
Use export default after defining the function to export it as default.
2
Create named export functions
Create a file called greetNamed.ts. Inside it, write two functions: greetMorning that returns 'Good morning!' and greetEvening that returns 'Good evening!'. Export both functions as named exports using export function.
Typescript
Hint
Use export function before each function to export them by name.
3
Import exports in main file
Create a file called main.ts. Import the default export from greetDefault.ts as greetDefault. Also import the named exports greetMorning and greetEvening from greetNamed.ts. Use the import syntax: import greetDefault from './greetDefault' and import { greetMorning, greetEvening } from './greetNamed'.
Typescript
Hint
Use default import syntax without braces and named import syntax with braces.
4
Call functions and print results
In main.ts, call the functions greetDefault(), greetMorning(), and greetEvening(). Print their returned strings using console.log.
Typescript
Hint
Use console.log to print each function's return value on its own line.
Practice
(1/5)
1. What is the main difference between default export and named exports in TypeScript?
easy
A. Default export requires curly braces when importing, named exports do not.
B. Default export allows exporting one main value per file, named exports allow multiple exports by name.
C. Named exports can only export functions, default exports can only export objects.
D. Default exports are deprecated in TypeScript, named exports are preferred.
Solution
Step 1: Understand default export purpose
Default export is designed to export a single main value from a file, like a main function or class.
Step 2: Understand named exports purpose
Named exports allow exporting multiple values by their names, so you can import only what you need.
Final Answer:
Default export allows exporting one main value per file, named exports allow multiple exports by name. -> Option B
Quick Check:
Default export = one main, Named exports = multiple [OK]
Hint: Default = one main export, named = many exports by name [OK]
Common Mistakes:
Confusing import syntax for default and named exports
Thinking default export can export multiple items
Assuming named exports require default keyword
2. Which of the following is the correct syntax to import a default export from a module named utils.ts?
easy
A. import utils from './utils';
B. import { utils } from './utils';
C. import * as utils from './utils';
D. import utils = require('./utils');
Solution
Step 1: Recall default import syntax
Default exports are imported without curly braces using the syntax: import name from 'module'.
Step 2: Check options for correct syntax
import utils from './utils'; matches this syntax exactly, importing the default export as utils.
Final Answer:
import utils from './utils'; -> Option A
Quick Check:
Default import has no braces [OK]
Hint: Default imports have no curly braces [OK]
Common Mistakes:
Using curly braces for default imports
Confusing import * as with default import
Using require syntax in ES modules
3. Given the following module mathUtils.ts:
export default function add(a: number, b: number) { return a + b; }
export function multiply(a: number, b: number) { return a * b; }