TypeScript Reference ยท Modules and Declaration Files
Re-exports in TypeScript
Learn TypeScript re-exports: export * from, export { x as y } from, export * as ns, type-only re-exports, barrel files and what export * leaves out.
A re-export publishes names from another module without first importing them into the local scope. It is how a folder gets one public entry point (often called a barrel file, usually index.ts) that gathers items from many small files.
There are four shapes: export * from "./m.js" re-exports every named export, export { a, b as c } from "./m.js" picks and renames, export * as ns from "./m.js" bundles a whole module under one name, and export type { T } from "./m.js" forwards only types. None of them creates a local variable; you cannot use the re-exported name inside the re-exporting file unless you also import it.
export * from "./shapes.js"; export { area as shapeArea } from "./shapes.js"; export * as math from "./math.js"; export type { Shape } from "./shapes.js";
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 * from "module"; export * as name from "module"; export { a, b as c } from "module"; export { default } from "module"; export { default as thing } from "module"; export type { T } from "module"; export type * from "module"; export type * as ns from "module";
| Form | Description |
|---|---|
export * from |
Re-exports all named exports. It skips default and any name the file declares or re-exports explicitly. |
export * as ns from |
Creates one named export ns holding the namespace object of the other module. |
export { x as y } from |
Selects and renames individual exports. |
export { default } from |
Forwards the default export as the default. |
export type { T } from |
Re-exports names for type positions only; erased from the output. |
export type * from |
Type-only star re-export (TypeScript 5.0+). |
Edge cases and errors:
- If two
export *statements provide the same name, that name is silently left out of the module in JavaScript. TypeScript reports TS2308 when it detects the ambiguity at a re-export. Add an explicitexport { name } fromto resolve it, as the second example shows. - Using a re-exported name locally needs a separate
import. - Circular re-exports are legal but can leave bindings undefined if evaluation order matters.
Usage notes
- Barrels make imports pretty (
import { a, b } from "./shapes"), but they can hurt build speed and tree shaking: importing one name may load every file behind the barrel. Large libraries often avoid deep barrels. - With
isolatedModulesorverbatimModuleSyntax, re-exporting a type needsexport type, because a tool that compiles one file cannot know whether the name is a type. export *is dynamic in ES modules: the list of names is computed when modules link.export { a } fromis static and gives tools exact information.- Under
nodenext, the specifier needs the real extension (written.jsin a normal TypeScript project). - A re-export keeps a live binding, not a copy: later changes to the original variable are visible through the barrel.