Named and Default Exports
Named and Default Exports — the core idea
A module can export many named bindings but at most one default export. A named import must use the exported name (unless aliased); a default import can choose its local identifier. Default exports are not inherently better for every library.
What you’ll learn
- Explain named and default exports with a concrete use case.
- Run the example and account for every line of its output.
- Recognize the difference between compile-time checking and runtime behavior.
Understand the rules and when to use them
Reading the code: The function is the default export and taxRate is a named export. The function multiplies 50 by 1.1 and toFixed formats the result as a two-decimal string.
Keep the boundary clear: A common mistake is writing import { defaultFunctionName } without checking whether the producer exported it by that name.
See the idea as a flow
Run the complete TypeScript example
Save the code as named-default-exports.ts. The result below was captured after compiling this exact example with strict TypeScript checking and executing the emitted JavaScript in Node.js.
TypeScript code
export const taxRate = 0.10;
export default function afterTax(price: number): number { return price * (1 + taxRate); }
console.log(afterTax(50).toFixed(2));Verified output
55.00Build: npx tsc --strict --target es2022 --module commonjs --lib es2022,dom,esnext.disposable named-default-exports.ts; run: node named-default-exports.js. The exact filename may be changed freely. When a multi-file project exists, prefer its tsconfig.json and build scripts.
Trace the example line by line
The function is the default export and taxRate is a named export. The function multiplies 50 by 1.1 and toFixed formats the result as a two-decimal string.
The output shown above comes from the initialized values in this program. Replacing a type annotation alone does not transform a JavaScript value.
Build the real multi-file example
This separate mini-project demonstrates real imports, exports, or declarations across files. Create each file in one folder, then compile and run main.js. The example was checked separately from the single-file program.
tax.ts
export const rate = 0.10;
export default function addTax(n:number){return n*(1+rate);}
main.ts
import afterTax, {rate} from "./tax";console.log(afterTax(100).toFixed(2),rate);
CommonJS setup: compile with npx tsc --strict --target es2022 --module commonjs main.ts and run node main.js. Compile from the same directory so relative modules are discovered. If this example contains helpers.js, keep that runtime file alongside the emitted main.js.
Verified output from the mini-project
110.00 0.1Avoid this common mistake
A common mistake is writing import { defaultFunctionName } without checking whether the producer exported it by that name.
Check both the TypeScript diagnostic and the emitted JavaScript behavior. A program can compile correctly and still need runtime validation or error handling.
Practice independently
Write a second file importing the default function under a different local name and taxRate as a named import.
Before running your change, write down the type you expect TypeScript to infer and the output you expect JavaScript to print. Then check both predictions.
Check your understanding
What to remember
A module can export many named bindings but at most one default export.
Try the practice change and re-run the exact program to test your understanding.
Reaching the recap and completing the quizzes are separate milestones.