TypeScript Reference ยท Modules and Declaration Files
Default and named exports in TypeScript
Compare default and named exports in TypeScript: syntax, how imports are typed, renaming, export default of types, and why named exports are easier to maintain.
A module can export any number of named values and at most one default value. Named exports are looked up by name, so the importer must use that name or rename it with as. A default export is just a named export whose name is default; the importer chooses any local name for it.
TypeScript treats both forms the same way for values, and extends them to types: interfaces and type aliases can be exported by name or as a default. The checker verifies that every imported name really exists in the target module and that you use it with the right type.
export const version = "1.0"; // named export function parse(text: string) {} // named export default class Parser {} // default
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Syntax
export const a = 1; export { b, c as d }; export default expression; export { value as default }; import def from "./m.js"; import { a, d as renamed } from "./m.js"; import def2, { a as a2 } from "./m.js"; import { default as def3 } from "./m.js";
| Form | Description |
|---|---|
export const x / export function f |
Named export declared inline. |
export { x, y as z } |
Named exports gathered in a list; as renames. |
export default expr |
The default export. It may be an expression, a function, a class or an interface. |
import d from |
Binds the default export to a local name you choose. |
import { x as y } |
Binds a named export under a different local name. |
Edge cases and errors:
export defaultcan followfunction,classandinterfacewith an optional name, butexport default const x = 1is a syntax error. Writeconst x = 1; export default x;.- A type alias cannot be declared inline as a default:
export default type T = stringis invalid. Declare it first, thenexport default T, or use a named export. - Importing a name that does not exist reports error TS2305: Module has no exported member. Default-importing a module with no default reports TS1192.
Usage notes
- Prefer named exports. Editors can auto-import and rename them, grep finds them, and every importer uses the same name. A default export lets each file invent a different name for the same thing.
- Default exports remain common for single-purpose files such as a React component, a config object or a CLI entry point, and some frameworks require them.
- When you export an anonymous default (
export default () => {}), stack traces and debuggers show an inferred or empty name. Give it a name. export * fromdoes not re-export the default. Useexport { default } from "./m.js"for that.- CommonJS packages have no real default export; how a default import maps to
module.exportsdepends on interop settings. See ESM and CJS interop. - Type-only exports use
export type, which is erased entirely; see Type-only imports.
Related
- ES modules, Re-exports, import and export
- Lessons: Modules