β Named Exports
You explicitly name what you're exporting. You can export multiple values per file.
πΈ Syntax:
// math.ts
export const PI = 3.14;
export function add(a: number, b: number): number {
return a + b;
}πΈ Importing:
import { PI, add } from './math';β Pros:
Encourages clarity (you see exactly what you're importing).
Allows importing multiple items from the same file.
IDE auto-completion and type-checking are better.
β Default Export
You export one main value from a module.
πΈ Syntax:
// config.ts
const API_URL = "<https://api.example.com>";
export default API_URL;πΈ Importing:
import API_URL from './config';You can rename the default import to anything during import.
βοΈ Key Differences
Feature | Named Export | Default Export |
|---|---|---|
Syntax |
|
|
How many per file? | Many | Only one |
Import syntax |
|
|
Can you rename? | Yes, with | Yes, without |
Auto-import in IDEs | More accurate | Sometimes less predictable |
π Example Comparing Both
// utils.ts
export const greet = (name: string) => `Hello, ${name}`;
export default function log(message: string) {
console.log(message);
}// main.ts
import log, { greet } from './utils';
log(greet("Codeveda")); // Output: Hello, Codevedaπ When to Use Which?
Use Case | Prefer This Export |
|---|---|
Utility functions | Named |
Only one main functionality | Default |
Shared constants/types | Named |
Library modules (e.g., React) | Default (often) |