The beginner local project introduced exports and imports. A type alias exists only during checking, so import it with import type { Book } from "./domain.js". Import runtime functions with an ordinary import such as import { decodeBook } from "./domain.js".
Why bother, if the compiler can tell types from values? With this course's project settings it can: tsc sees all the files, notices Book is only a type, and drops it from the emitted import even if you wrote a plain import { Book }. But not every tool sees all the files. Node's built-in type stripping (node main.ts), and bundlers that compile one file at a time, only delete syntax they can recognize as type-only. Given import { Book, label } from "./domain.ts", Node keeps Book in the import and fails at startup with SyntaxError: The requested module './domain.ts' does not provide an export named 'Book'. import type makes the erasure visible in the source, so every tool agrees. The compiler option verbatimModuleSyntax enforces this: it reports TS1484: 'Book' is a type and must be imported using a type-only import for a plain import of a type.
A runtime class is different: a class can be both a constructor value and an instance type, so it needs an ordinary import. You do not need classes for this catalogue.
The browser editors are independent single-file modules. Combine definitions in one box; do not paste relative project imports into them. Day 7's download keeps domain.ts, service.ts and main.ts separate, with .js import extensions resolved to .ts source by the NodeNext compiler settings.
For example, domain.ts can export both type Book = { title: string } and a function label accepting Book. A caller imports label as a value and Book as a type. The self-contained equivalent below runs the same function without the file boundary.
The actual file boundary
These two listings belong in separate local files, not the single-file browser editor. The .js spelling names the emitted file Node will execute; the compiler finds domain.ts while checking.
domain.ts
export type Book = { title: string };
export function label(book: Book): string {
return book.title;
}main.ts
import type { Book } from "./domain.js";
import { label } from "./domain.js";
const book: Book = { title: "Modules" };
console.log(label(book));After compilation, the Book import and annotations disappear. The label import, record and console call remain. Day 7 supplies a complete configured project so you can check this locally.
Predict, run, and explain
Write down what the program prints before you run it. Use Check types first; the program runs only when the real compiler accepts it.
type Book = { title: string };
function label(book: Book): string {
return book.title;
}
const book: Book = { title: "Modules" };
console.log(label(book));Enable JavaScript to edit and type-check this example.
Check your prediction
ModulesRead the compiler error
The spelling names a type, not a runtime constructor or record.
type Book = { title: string };
console.log(Book);Enable JavaScript to edit and type-check this example.
Check your diagnosis
Print a Book value instead. Type aliases are erased; they cannot be used as values.
Types disappear while runtime collaborators remain
A type alias helps check source but does not become a JavaScript constructor. A runtime function can use that alias in its contract, yet still needs a real implementation. Keeping the two roles separate prevents attempts to print or import a type as if it were a value.
The local project splits declarations into modules; the browser version combines the same definitions into one file. Do not delete necessary runtime imports merely because type imports disappear. A decoder function must still be present when emitted code calls it.
type Book = { title: string };
function label(book: Book): string {
return book.title.trim();
}
const book: Book = { title: " Types " };
console.log(label(book));
console.log(typeof label);Enable JavaScript to edit and type-check this example.
Check your prediction
Types
functionBook and the annotations are erased. Label, the object and trim still exist and run. The type did not implement the normalization.
type Book = { title: string };
function label(book: Book): string {
return book.title;
}
const sameFunction = label;
console.log(sameFunction === label);
console.log(sameFunction({ title: "Modules" }));Enable JavaScript to edit and type-check this example.
Check your prediction
true
ModulesAn imported runtime function is still a function value, not a copied implementation generated by its parameter type. Module boundaries determine where it is supplied, while the contract checks how it is called.
Try the rule on new inputs
In the download, mark each import as type-only or runtime. Inspect emitted service.js and explain why the decoder import remains. Then change one import type into a plain import and rebuild: the emitted file is unchanged, because tsc sees the whole project. Add "verbatimModuleSyntax": true to tsconfig.json and rebuild again to see TS1484. Keep relative imports out of the single-file browser exercise.
Your turn
Modules & Type Imports: practice
Here is a two-file program. It runs under tsc with this course's settings, but a teammate wants to enable verbatimModuleSyntax and run it with Node's type stripping.
domain.ts
export type Shelf = "A" | "B";
export type Book = { title: string; shelf: Shelf };
export const defaultShelf: Shelf = "A";
export function label(book: Book): string {
return `${book.title} (shelf ${book.shelf})`;
}main.ts
import { Book, Shelf, defaultShelf, label } from "./domain.js";
const shelf: Shelf = defaultShelf;
const book: Book = { title: "Types", shelf };
console.log(label(book));On paper: (1) classify each of the four imported names as type-only or value; (2) rewrite main.ts's imports so they compile under verbatimModuleSyntax; (3) predict the single import line left in the emitted main.js. Then, in the workspace, combine both files into one program (no import lines) and run it to confirm it prints Types (shelf A).
// Combine domain.ts and main.ts into one program here (drop export and import).Enable JavaScript to edit and type-check this example.
Hint 1: the idea
Ask of each name: does anything exist at runtime under this name? A type declaration never does; a const or function always does, even if its own annotation uses a type.
Hint 2: a first step
defaultShelf is annotated with Shelf, but it is still a const holding "A". Sort the four names into two groups, then write one import type line and one ordinary import line.
Show one solution
type Shelf = "A" | "B";
type Book = { title: string; shelf: Shelf };
const defaultShelf: Shelf = "A";
function label(book: Book): string {
return `${book.title} (shelf ${book.shelf})`;
}
const shelf: Shelf = defaultShelf;
const book: Book = { title: "Types", shelf };
console.log(label(book));Enable JavaScript to edit and type-check this example.
Types (shelf A)Book and Shelf are type-only; defaultShelf and label are values. The imports that compile under verbatimModuleSyntax are:
import type { Book, Shelf } from "./domain.js";
import { defaultShelf, label } from "./domain.js";and the emitted main.js keeps exactly one of them: import { defaultShelf, label } from "./domain.js";. The original single import gets TS1484 for Book and Shelf under that option, and under Node's type stripping it fails at startup because domain has no runtime export named Book. The checker can classify the names for you; only running the program shows that label still formats the record correctly.
Change it and test it
In a local copy, export Book and pageLabel from one module and import them with the appropriate type/value forms. Run checking before execution. Explain why typeof Book cannot inspect a type alias at runtime.
Completion is saved in this browser only. It does not measure mastery.