TypeScript Reference ยท Keywords and Declarations
The import and export keywords
Reference for TypeScript import and export: named, default and namespace imports, re-exports, dynamic import(), and how types travel between modules.
import and export are the standard JavaScript module syntax, and TypeScript uses them unchanged with one addition: types and interfaces can be exported and imported next to values. Any file with a top-level import or export is a module with its own scope; a file without one is a script whose declarations are global.
Which specifiers you write, whether you add file extensions, and whether the output is ESM or CommonJS depend on the module and moduleResolution settings. Under nodenext, relative imports in ES modules need the extension of the emitted file, such as ./util.js.
import { readFileSync } from "node:fs"; export const answer = 42; export default function greet() {}
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
import def from "m"; import def, { a, b as c } from "m"; import * as ns from "m"; import "m"; // side effects only import { type T, v } from "m"; // inline type modifier const m = await import("m"); export const x = 1; export { a, b as c }; export default expression; export { a as default }; export * from "m"; export * as ns from "m"; export { a } from "m";
| Form | Description |
|---|---|
import def from |
Binds the module's default export. |
import { a as b } |
Named imports, optionally renamed. Bindings are live and read-only. |
import * as ns |
A namespace object containing every named export. |
import "m" |
Runs the module for its side effects and imports nothing. |
export * |
Re-exports every named export except default. |
import() |
Returns a promise of the module namespace. |
Edge cases and errors:
- Imported bindings cannot be reassigned; the exporting module changes them, and importers see the new value.
- A default export cannot be combined with
export *: it is never re-exported that way. - Importing a name the module does not export is error TS2305; a missing module is TS2307.
- Under
nodenexta relative specifier without an extension is an error in ES modules (TS2835), andimport x froma CommonJS-only package may needesModuleInterop. - A module declared with
export =(such asnode:path) can be imported withimport x fromorimport * as x, but it cannot be re-exported withexport *(error TS2498). - Circular imports work, but a binding read before its module has run is in the temporal dead zone.
Usage notes
- Prefer named exports. They rename cleanly, autocomplete well and avoid the "what did I call it" problem of defaults.
- With
verbatimModuleSyntax, imports that are only types must useimport typeor the inlinetypemodifier, otherwise they stay in the output. export defaultaccepts an expression; the declaration forms (export default class,export default function) create a name in the module.import.metaexists in ES modules only, and top-levelawaitneedsmoduleset toes2022,esnext,node16ornodenexttogether with a target of ES2017 or newer.- Barrel files that re-export everything make import graphs bigger and can hurt tree shaking and build time.