Sub topic 2
Exporting and Importing: Named, Default, and Namespace Forms
Assume you know basic TypeScript types, interfaces, functions, and JavaScript import and export statements. You should also be able to run tsc and read a basic tsconfig.json.
A module is a file that exports or imports something. Its declarations stay inside that file unless you explicitly export them. Other modules can then import the exported values or types.
Named exports and imports
A named export has a specific name. A module can have many named exports.
// math.ts
export function add(a: number, b: number): number {
return a + b;
}
export const PI = 3.14159;Import named exports by placing their names inside braces:
// main.ts
import { add, PI } from "./math.js";
console.log(add(1, 2));
console.log(PI);The imported name must match the exported name. You can rename it with as:
import { add as sum } from "./math.js";
console.log(sum(4, 5));Named exports make the relationship between files clear. They are often a good default for library code because each exported item has an explicit name.
Default exports and imports
A module can have one default export. It represents the module's primary value.
// multiply.ts
export default function multiply(a: number, b: number): number {
return a * b;
}Import a default export without braces. The importing file chooses the local name:
import multiplyNumbers from "./multiply.js";
console.log(multiplyNumbers(2, 3));The local name does not need to match multiply. This flexibility can make a module easy to consume, but it can also make a large codebase harder to search. Two files might import the same default export under different names.
A file may combine one default export with named exports:
// math.ts
export function add(a: number, b: number): number {
return a + b;
}
export const PI = 3.14159;
export default function multiply(a: number, b: number): number {
return a * b;
}Import both forms in one statement:
import multiply, { add, PI } from "./math.js";
console.log(add(1, 2), multiply(2, 3), PI);| Export form | Import form | How many per module |
|---|---|---|
| `export const x` | `import { x }` | Many |
| `export default value` | `import value` | One |
| Any exports | `import * as name` | One namespace object |
Namespace imports
A namespace import collects the module's named exports into one object-like value:
import * as math from "./math.js";
console.log(math.add(3, 4));
console.log(math.PI);This form is useful when a module exports related operations and you want to keep their names grouped. The namespace does not normally include the default export as a direct named property in the way you should expect for ordinary named exports. Import a default export separately when you need it.
In Node.js ES modules, relative import paths commonly include the .js extension even when the source file is .ts. TypeScript checks the source path while the emitted JavaScript uses .js.
The import relationships can be pictured like this:
Exercise
Which import correctly imports the named export add and the default export multiply from ./math.js?
Show answer
import multiply, { add } from "./math.js";