Concept Flow - Default exports vs named exports
Create Module File
Choose Export Type
Default Export
Import Module
Use Exported Value
Shows how a module file exports values either as default or named, and how importing modules access them.
export default function greet() { return 'Hello!'; } export function bye() { return 'Goodbye!'; }
| Step | Action | Export Type | Exported Name | Import Syntax | Resulting Import Value |
|---|---|---|---|---|---|
| 1 | Define greet function | default | greet | import greet from './module'; | greet function available |
| 2 | Define bye function | named | bye | import { bye } from './module'; | bye function available |
| 3 | Import default as named | default | greet | import { greet } from './module'; | Error: greet not found |
| 4 | Import named as default | named | bye | import bye from './module'; | Error: default export missing |
| 5 | Import both correctly | default + named | greet + bye | import greet, { bye } from './module'; | Both functions available |
| Variable | Start | After Step 1 | After Step 2 | After Step 5 |
|---|---|---|---|---|
| greet | undefined | function greet() | function greet() | function greet() |
| bye | undefined | undefined | function bye() | function bye() |
Default exports: one per module, imported without braces.
Named exports: multiple per module, imported with braces.
Import syntax must match export type.
Mix default and named imports with: import defaultName, { named1 } from 'module';
Errors occur if syntax mismatches exports.default export and named exports in TypeScript?utils.ts?import name from 'module'.utils.mathUtils.ts:
export default function add(a: number, b: number) { return a + b; }
export function multiply(a: number, b: number) { return a * b; }
What will be the output of this code?
import add, { multiply } from './mathUtils';
console.log(add(2, 3));
console.log(multiply(2, 3));add is imported without braces, and the named export multiply is imported with braces.add(2, 3) returns 5, and multiply(2, 3) returns 6, so console logs 5 and 6.export default function greet() { return 'Hello'; }
export function greet() { return 'Hi'; }
What is the problem here?greet, which causes a conflict.shapes.ts exporting multiple shapes and a default export for the main shape:
export interface Circle { radius: number; }
export interface Square { side: number; }
const defaultShape = { type: 'circle', radius: 5 };
export default defaultShape;
How would you import both the default shape and the Square interface correctly in another file?defaultShape is imported without curly braces.Square interface is imported with curly braces.Pass the test and this pattern moves unknown