liveBook · Manning

Named import

As explained in Secrets of the JavaScript Ninja, Third Edition

A named import brings a specific named export into another module, optionally under a local alias.

A named import selects exports from another module using their exported names. Use `as` to give an import a different local name, including when two modules export the same name. section 13.1.1

Listing 13.2 Importing named values
import { #A
  homeIsland, #A
  getOccupation as getTitle, #A
} from "./1-export-syntax.ts"; #A

console.assert(
  homeIsland === "Tsushima",
  "The home island is Tsushima",
);
console.assert(
  getTitle("Yotei") === "Ghost of Yotei",
  "The protector of Yotei is a ghost",
);

The example imports `homeIsland` unchanged and aliases `getOccupation` as `getTitle`. section 13.1.1

How Secrets of the JavaScript Ninja, Third Edition covers it

How deep it goes
Explains brace syntax, aliasing with `as`, and name-conflict resolution, then notes runtime-dependent file-extension rules.
Examples
Demonstrates importing `homeIsland` and aliasing `getOccupation` as `getTitle`.
What you can do after
Apply named imports with aliases, while checking runtime-specific file-extension and TypeScript configuration requirements.
How it connects
Connects named imports to named exports, default imports, and namespace imports.

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.