Moving to Types — TypeScript

Move a PAUD’s app to TypeScript: strict, overloads, #private, decorators, .d.ts, NodeNext. 16 levels, played right in the browser with nothing to install.

What you learn in this game

At PAUD Teratai, Mbak Okta is moving Buku Teratai, the JavaScript app her cousin left behind, to TypeScript one step at a time. Every morning the app runs as it does on the PAUD’s computer: tsc reads the project first, then Node runs what was built and prints the slips for the parents. If tsc finds an error, the slip is held at the desk and nothing prints. If tsc says nothing, Node prints whatever the code produces, right or wrong. The judges are the real tsc and the real Node: the error codes, their lines, and what the slips say.

In week two Mbak Okta sorts out Buku Teratai’s data types. From today Mas Ilham, the PAUD’s volunteer, writes code too, and his changes arrive next week. Besides this morning, Run also builds the project with Mas Ilham’s changes: his mistakes must be held by tsc, not reach the parents.

A function sometimes takes input of different shapes, and its output differs with it. Overloads write each pair as its own signature above one implementation, and tsc picks the signature that fits each call.

In week three Buku Teratai uses classes: pick-up cards, bills, kuitansi. Not everything TypeScript writes in a class reaches the JavaScript. Some of it is only read by tsc and then removed; some of it is still there when Node runs it.

A decorator is a function written with @ above a method, class or field, which wraps it with extra behaviour. TypeScript 5 and later use the standard ECMAScript decorators; older libraries use the experimentalDecorators kind, which has a different shape.

In week four Buku Teratai uses libraries from npm and runs as a whole project. Many older libraries are written in CommonJS JavaScript with no types. A .d.ts declaration file describes their shape to tsc without rewriting the library.