Use named exports
In cart Checkout
When to apply
Before writing, changing, or reviewing TypeScript module exports and imports.
Export values and types by name, and avoid default exports except where a framework requires them.
Implementation
- Write
export function formatCurrency()and import it asimport { formatCurrency } from './format-currency'. - Allow default exports only where a framework requires them, such as some file-based routing pages or configuration files, through a lint override for those paths.
- Enforce with
import/no-default-export.
This is a consistency convention.
Rationale
A named export has one name that every import must use, so searching finds every use, and a rename tool updates them all. A default export can be imported under any name, which scatters different names for the same thing, and a missing default export is not always caught.
Examples
Incorrect (counterexample):
export default function formatCurrency(amount: number): string {
return amount.toFixed(2);
}
import formatMoney from './format-currency';
Correct:
export function formatCurrency(amount: number): string {
return amount.toFixed(2);
}
import { formatCurrency } from './format-currency';
Validation
Run the lint rule, and check that its overrides cover only paths where a framework requires default exports.
A default export required by a framework is not a violation.