Skip to content
public-library›TypeScripttechs/typescript

Use named exports

Sign in to star Use named exports, 0 stars

Sign in to star rules

Star this rule if you find it useful.

Add to cart

What would you like to add?

At checkout, you'll pick which project these go into, and you can fork a rule instead of staying in sync.

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 as import { 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.