liveBook · Manning

Namespace import

As explained in Secrets of the JavaScript Ninja, Third Edition

A namespace import binds a module's exported names together as properties of an imported namespace object.

A namespace import gathers a module’s exports behind one imported name. Access each export as a property, including a default export under the `default` property. section 13.1.1

Listing 13.6 Importing a module as a namespace
import * as characters from "./4-default-export.ts"; #A

console.assert(
  characters.default === "Ranma",
  "The default export is Ranma",
);
console.assert(
  Object.keys(characters).length === 3,
  "All 3 exports are included in the namespace",
);

The `characters` namespace contains all three exports; the default export is available as `characters.default`. section 13.1.1

How Secrets of the JavaScript Ninja, Third Edition covers it

How deep it goes
Explains namespace syntax and property access, including the default export.
Examples
Uses a characters namespace containing a default export and two named exports.
What you can do after
Apply it via ESM import syntax to access a module’s exports through one namespace object.
How it connects
Connects namespace imports with ES modules, named exports, and default exports.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Secrets of the JavaScript Ninja, Third Edition. The section links point to the parts of the book it draws on.