Importing Modules
Importing Modules — the core idea
An import creates a binding to an exported value or type from another module. Named imports, aliases, and namespace imports are distinct forms. Modules are resolved at build or run time according to the chosen toolchain.
What you’ll learn
- Explain importing modules with a concrete use case.
- Run the example and account for every line of its output.
- Recognize the difference between compile-time checking and runtime behavior.
Understand the rules and when to use them
Reading the code: This producer module exports two named values. A consumer can write `import { area as circleArea } from "./geometry.js"`; local aliasing does not rename the producer's export.
Keep the boundary clear: An import only works when the specifier resolves to the intended emitted file or installed package; type checking alone is not a module loader.
See the idea as a flow
Run the complete TypeScript example
Save the code as importing-modules.ts. The result below was captured after compiling this exact example with strict TypeScript checking and executing the emitted JavaScript in Node.js.
TypeScript code
export const PI = 3.14159;
export function area(radius: number): number { return PI * radius * radius; }
const areaOfThree = area(3);
console.log(areaOfThree.toFixed(2));Verified output
28.27Build: npx tsc --strict --target es2022 --module commonjs --lib es2022,dom,esnext.disposable importing-modules.ts; run: node importing-modules.js. The exact filename may be changed freely. When a multi-file project exists, prefer its tsconfig.json and build scripts.
Trace the example line by line
This producer module exports two named values. A consumer can write import { area as circleArea } from "./geometry.js"; local aliasing does not rename the producer's export.
The output shown above comes from the initialized values in this program. Replacing a type annotation alone does not transform a JavaScript value.
Build the real multi-file example
This separate mini-project demonstrates real imports, exports, or declarations across files. Create each file in one folder, then compile and run main.js. The example was checked separately from the single-file program.
geometry.ts
export function area(r:number){return 3.14*r*r;}
main.ts
import {area as circleArea} from "./geometry"; console.log(circleArea(2).toFixed(2));
CommonJS setup: compile with npx tsc --strict --target es2022 --module commonjs main.ts and run node main.js. Compile from the same directory so relative modules are discovered. If this example contains helpers.js, keep that runtime file alongside the emitted main.js.
Verified output from the mini-project
12.56Avoid this common mistake
An import only works when the specifier resolves to the intended emitted file or installed package; type checking alone is not a module loader.
Check both the TypeScript diagnostic and the emitted JavaScript behavior. A program can compile correctly and still need runtime validation or error handling.
Practice independently
Create geometry.ts and main.ts using a named import and an alias; compile with the runtime's module mode.
Before running your change, write down the type you expect TypeScript to infer and the output you expect JavaScript to print. Then check both predictions.
Check your understanding
What to remember
An import creates a binding to an exported value or type from another module.
Try the practice change and re-run the exact program to test your understanding.
Reaching the recap and completing the quizzes are separate milestones.