bhylu.com

Advertisements

TypeScript Tooling and Type Tests

Use a compiler check, runtime tests and editor feedback for their different jobs. A bundler can remove types while still allowing semantic mistakes that a separate tsc check would reject.

How it works

The course runs a strict compiler pass before executing each complete source. Companion rejection fixtures deliberately contain invalid assignments or calls and must fail checking. That gives type behavior its own tests. In a small project, scripts for check, build and test make the order explicit; a larger build may use project references or a test framework, but the same separation remains useful.

Run the code

Try in playground loads this source without running it. Choose Run when ready. The existing TypeScript 5.8.3 worker transpiles and executes one file; it does not perform full semantic type checking, load npm imports or provide DOM/Node APIs. Use the local compiler for the type contract.

For a full check, save the three downloads together in a new folder, run npm install, then npm run check. Use npm run build followed by npm start to execute the checked JavaScript with Node. The supplied project enables strict checking, indexed-access checks and exact optional properties.

Example

lesson.ts

type Desk = "north" | "east";
function limitFor(desk: Desk): number { return desk === "north" ? 6 : 4; }
console.log(limitFor("east"));

Expected result

4

The runtime result checks the selected rule. The separate invalid fixture calls the function with an unsupported desk and must produce a compiler error. @ts-expect-error can document intentional negative assertions in a real type-test file, but broad suppression in application code hides defects rather than testing them.