Skip to content

Modules & Compiler Settings

The Course
•
10 min study

Optional — needs Node.js 22+ and npm on your computer. Every exercise in this course runs in the browser, including the one at the end of this lesson. If you don't have Node installed, read the commands, do the browser exercise, and come back to the download later; the beginner lesson Running TypeScript Locally walks through installing it.

Use Node.js 22+ and npm. Extract the catalogue ZIP and open a terminal inside catalogue. Run npm install once, then npm run check, npm start and npm test. Start prints Types. Checking uses TypeScript 5.9.3, the same version as the browser course.

Read the imports directly: service.ts imports domain.ts; main.ts imports both service.ts and domain.ts. Domain exports runtime functions and types, service imports the decoder as a value and Book/Result as types, and main invokes the configured loader. Ordinary imports and import type have different runtime meaning, as taught on Day 1.

The compiler uses strict, noUncheckedIndexedAccess, exactOptionalPropertyTypes and noEmitOnError. NodeNext resolves .js imports to .ts source and emits .js modules for Node. Keep generated dist and installed node_modules out of authored source.

The browser example combines the same definitions into one module because it does not resolve local project files. Do not “fix” a project import by deleting its .js extension without understanding the target environment. TypeScript configuration changes should be deliberate and checked alongside runtime execution.

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.

Predict, then runTypeScript
const stages: readonly [string, string, string] = ["domain", "service", "main"];

console.log(stages.join(" -> "));

Enable JavaScript to edit and type-check this example.

Check your prediction
Text
domain -> service -> main

Trace commands and modules rather than directory order

The compiler checks source dependencies and emits JavaScript; Node runs the emitted main entry. Domain owns validation and state rules, service owns the injected transport adapter, and main supplies orchestration. File listing order is not an execution plan. Follow the actual imports and function calls.

Compile-only contracts deliberately contain misuse and must not run as the application. Keep their checks separate from the behavior suite, which exercises normalized data, failures and schedules through emitted code.

Boundary or caseWhat to inspect
CheckVerify source and focused negative contracts
StartRun emitted main with the deterministic transport fixture
TestCheck types and then validation, transitions and controlled schedules

Change only a transport fixture and predict valid, invalid-data and rejection results. Then inspect emitted imports: runtime decoder imports remain while type imports disappear. Recheck both checking and execution after changing module settings.

Your turn

Exercise

Modules & Compiler Settings: practice

The project's tsconfig.json adds two flags beyond strict: noUncheckedIndexedAccess and exactOptionalPropertyTypes. The browser editor uses the same settings. The program below would compile under strict alone. Before clicking Check types, predict which line each extra flag rejects and why. Then fix both lines without turning a flag off and without ! or as, so the program prints TYPES and no subtitle. If you have the download, turn each flag off in tsconfig.json and run npm run check to confirm which error belongs to which flag.

Find and fix the type errorTypeScript
type Book = { title: string; subtitle?: string };

const books: Book[] = [{ title: "Types" }];
const draft: Book = { title: "Maps", subtitle: undefined };

console.log(books[0].title.toUpperCase());
console.log(draft.subtitle ?? "no subtitle");

Enable JavaScript to edit and type-check this example.

Hint 1: the idea

One flag is about reading by index: does books[0] always exist? The other is about optional properties: is "absent" the same as "present with the value undefined"?

Hint 2: a first step

For the draft, decide whether the subtitle should be absent; for the lookup, save books[0] in a variable and handle undefined before reading its title.

Show one solution
One possible solutionTypeScript
type Book = { title: string; subtitle?: string };

const books: Book[] = [{ title: "Types" }];
const draft: Book = { title: "Maps" };

const first = books[0];
console.log(first === undefined ? "no books" : first.title.toUpperCase());
console.log(draft.subtitle ?? "no subtitle");

Enable JavaScript to edit and type-check this example.

Text
TYPES
no subtitle

noUncheckedIndexedAccess makes books[0] a Book | undefined, so .title on it was TS2532 ("Object is possibly 'undefined'"); the fix checks for an empty list instead of assuming one item. exactOptionalPropertyTypes made subtitle: undefined a TS2375 error, because subtitle?: string means "may be absent", not "may hold undefined"; omitting the property says what was meant. Neither flag changes the emitted JavaScript: they only make the checker refuse code that the default strict mode accepts, which is why the project turns them on.

Change it and test it

Run the download locally if possible and inspect the emitted service.js: its import type statements should be absent while its decoder import remains. Keep compile-only negative tests out of runtime entry points. Explain which settings make this project stricter than merely stripping type annotations.

Was this article helpful?

Your response is saved on this device.