ES modules
Export
Export shares functions, classes, and constants from one file. Other files can only use what the source file chooses to export. In math.ts:
export function add(a: number, b: number): number { return a + b; } export const version: string = "1.0";
A list in braces exports names that are declared in the file, so the declarations can stay plain.
Import
Import brings exported names into the current file. Named imports list each name in braces and must match the export spelling. In main.ts:
import { add, version } from "./math.js"; console.log(add(2, 3)); console.log(version);
5
1.0
Default exports
A module can have one default export for its main value. Default imports skip the braces and may choose any local name. In greet.ts:
export default function greet(name: string): string { return "Hello " + name; }
The file that uses it imports the default without braces:
import greet from "./greet.js"; console.log(greet("Ada"));
Hello Ada
Extensions in imports
With Node style resolution, relative imports keep the compiled .js extension even though the source file on disk is a .ts file. The compiler maps the .js path back to the matching source.
Write math.js in the import while editing math.ts, and the build will resolve it correctly.
import { add } from "./math.js"; const total = add(2, 3); console.log(total);
5
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does the
exportkeyword do to a declaration?Answer
It lets other files import the name
An export is the door of a module: only exported names can be imported elsewhere.
-
How is a named import written?
Answer
In braces, with the spelling the module exported
Named imports list each exported name in braces, and the spelling must match.
-
How many default exports can one module have?
Answer
Exactly one
The default export is the module's single main value, so a second one is an error.
-
Why does
import { add } from "./math.js"work when the source file on disk ismath.ts?Answer
Node resolution maps the compiled path back to the source
The import names the file the compiler will emit, and the compiler resolves that path to the TypeScript source.
-
What does this program print?
export const version: string = "1.0"; console.log(version);Answer
1.0
An export only marks the name for other files; the file still runs and logs its value.
-
What does this program print?
export function double(n: number): number { return n * 2; } console.log(double(4));Answer
8
The exported function is also a normal function in its own file, and 4 doubled is 8.
-
What does this program print?
export default function greet(name: string): string { return "Hello " + name; } console.log(greet("Ada"));Answer
Hello Ada
A named default export keeps its name inside the file, so the call adds the greeting.
-
What does this program print?
const add = (a: number, b: number): number => a + b; export { add }; console.log(add(2, 3));Answer
5
The export list marks an existing name, and the arrow function adds the two numbers.
-
What does the compiler say about this program?
export default function one(): number { return 1; } export default function two(): number { return 2; } console.log(one());Answer
error TS2323: Cannot redeclare exported variable 'default'.
The second default export redeclares the module's single default slot.
-
What does the compiler say about this program?
export const a = 1; export const a = 2; console.log(a);Answer
error TS2451: Cannot redeclare block-scoped variable 'a'.
Two
constdeclarations of the same name in one file are a duplicate binding.
Article author: Arthur Isaev