What needs Node: the project steps on this page need Node.js 22+ and npm on your computer. The predict step and the exercise run in the browser.
Install Node.js 22+ and npm, extract event-gateway.zip, and open a terminal inside event-gateway. Run npm install once, then npm run check, npm start and npm test. Start prints Types and true. All data is local; no account, API key, server or network transport is needed.
Read domain.ts first, then channel.ts and gateway.ts. Index.ts deliberately exports the supported surface. Main.ts is one caller. The private package.json resolves its own name through exports, so consumer.ts can import typed-event-gateway and check the generated .d.ts boundary without uploading anything.
The build uses strict, noUncheckedIndexedAccess, exactOptionalPropertyTypes, declaration and noEmitOnError. NodeNext resolves .js imports in TypeScript source to the corresponding .ts files, then emits .js for Node and declarations for TypeScript consumers. Keep dist and node_modules out of authored source.
If checking fails, fix the source and rebuild rather than editing dist. Public-type tests run after declaration generation, and runtime tests execute generated JavaScript. These are complementary checks: an export can disappear even when the internal implementation still works.
Predict, check, and run
Write the output before running. Use Check types first, then Check & run. Types disappear at runtime: explain both what the compiler checks and what the code actually does. Each editor is an independent ES module; await asynchronous work explicitly.
const commands = ["npm install", "npm run check", "npm start", "npm test"];
console.log(commands.join(" -> "));Enable JavaScript to edit and type-check this example.
Check your prediction
npm install -> npm run check -> npm start -> npm testInspect the declaration path a public caller sees
The build has two outputs: JavaScript for execution and declarations for checking. consumer.ts imports the package name through exports, so it sees the declared public entry instead of reaching into internal source. Re-exporting an implementation and making it discoverable to a consumer are related but separate packaging steps.
A missing public export may already fail while building main.ts. Repair the source entry, rebuild, and only then interpret the separate consumer check. Generated declarations are evidence of the source build, not another implementation to maintain manually.
| Boundary or case | What to inspect |
|---|---|
| index.ts | Forward supported runtime and type exports |
| package exports | Select .d.ts for checking and .js for ES module execution |
| consumer.ts | Check valid and intentionally invalid calls without running misuse |
Follow createGateway from source re-export through generated declaration to consumer import. Inspect one erased type-only import in emitted JavaScript. Never execute the negative consumer merely to observe that its invalid operations fail.
Build it yourself
Modules & Local Use: practice
The declarations lesson asked what reaches the .d.ts file. This is the other output: write by hand, in a comment under the code, the JavaScript that tsc emits for this module (ES2022 target, ES modules). Then run the module and check that your JavaScript would print the same lines. With the download, you can confirm your answer in dist after npm run build.
export type Stage = "source" | "declarations" | "consumer";
type Step = { readonly stage: Stage; readonly command: string };
export const steps = [
{ stage: "source", command: "npm run build" },
{ stage: "consumer", command: "npm run check" }
] as const satisfies readonly Step[];
export function describe(step: Step): string {
return `${step.stage}: ${step.command}`;
}
for (const step of steps) {
console.log(describe(step));
}
// Write the emitted JavaScript here, as a comment.Enable JavaScript to edit and type-check this example.
Hint 1: the idea
The JavaScript keeps everything that runs and nothing that only checks. Go through the module line by line and ask, for each piece, "would deleting this change what Node does?"
Hint 2: a first step
Start with the three kinds of type-only syntax here: type aliases (exported or not), annotations on parameters and results, and the as const satisfies ... suffix. All three disappear completely. The export keyword on a value stays.
Show one solution
export type Stage = "source" | "declarations" | "consumer";
type Step = { readonly stage: Stage; readonly command: string };
export const steps = [
{ stage: "source", command: "npm run build" },
{ stage: "consumer", command: "npm run check" }
] as const satisfies readonly Step[];
export function describe(step: Step): string {
return `${step.stage}: ${step.command}`;
}
for (const step of steps) {
console.log(describe(step));
}Enable JavaScript to edit and type-check this example.
source: npm run build
consumer: npm run checkThe emitted gate.js:
export const steps = [
{ stage: "source", command: "npm run build" },
{ stage: "consumer", command: "npm run check" }
];
export function describe(step) {
return `${step.stage}: ${step.command}`;
}
for (const step of steps) {
console.log(describe(step));
}Both aliases are gone, including the exported Stage: a type export exists only in the .d.ts file. as const satisfies readonly Step[] leaves no trace, so the array is an ordinary mutable array at runtime; as const made it readonly only to the checker. Annotations vanish from describe, and the template literal and loop are copied as written, because ES2022 already supports them. The exported names in the JavaScript, steps and describe, are what an import in Node can actually receive.
Change it and test it
Inspect dist/index.d.ts and dist/index.js side by side, as you did for gate.ts. Identify which re-exports exist only for checking. Temporarily remove one public export and run check; a source consumer such as main.ts may report it during the build before the separate consumer step. Restore it in source, then confirm both checking stages pass.
Completion is saved in this browser only. It does not measure mastery.