In this lesson, you renamed two of the three files in the ticketing slice to TypeScript, one module at a time. After every step, npm run check passed and the code ran, with strict still off.
Key Points
- Follow the imports. Draw each import as an arrow from the importing file to the file it imports from.
returns-waitlist.jsandticket-types.jsboth import fromprices.js, which imports nothing from the project. A module like that, at the end of the arrows, is a leaf of the import graph. - Start at a leaf and work inward. A file’s types depend on what it imports, never on what imports it. Renamed in that order, in a graph without loops, every new
.tsfile imports only modules that are already TypeScript. Two modules that import each other get renamed together. - A wrong order can hide unchecked calls. In this small slice, another order would pass too, since
prices.jsalready has JSDoc. In the full ticketing pages, say the basket imports a module whose parameters have no types and nothing to infer them from. Those parameters areany, so the basket looks finished while its calls go unchecked until someone types that module. - Turn JSDoc into annotations. In a
.tsfile, the checker ignores JSDoc types, so after the rename,lineTotal()would take a string again without an error. Move each@paramtype onto its parameter and each@returnstype after the parentheses, then delete the comments. - Update the imports that name the file, by hand. The check won’t catch an old name, because it maps a
.jsimport such as"./prices.js"to the.tsfile that would compile to it. But Node and the browser load the exact file named, so"./prices.js"becomes"./prices.ts"in both importers. "allowImportingTsExtensions": truelets an import name a.tsfile. It works only in a config that compiles nothing to JavaScript, as"noEmit": truesays here. Node and Vite run the files as they are.- Type the boundary first.
returns-waitlist.jshas no JSDoc yet, so its types go straight into the code. Its exported functions are its boundary. The ticketing pages call them, and every call is checked against their annotations. - A type’s fields come from the code and the data.
refundFor()readspaidCentsandseatsfrom a booking, and bookings at the box office also carry areferenceand aname, soBookinglists all four.offerToWaitlist()returns anOffer, a waitlist entry plus the seats offered, ornullwhen nobody waiting fits. Withstrictoff, the checker doesn’t hold callers to thatnullyet. - Inside the functions, the checker infers the rest.
waitlistholdsWaitlistEntryobjects, soentryin the callback is one too. - JavaScript and TypeScript can sit side by side.
ticket-types.jsstays JavaScript for now. It importslineTotal()fromprices.ts, andcheckJschecks its call against the new annotations. - Run the code after every step, too. The check can’t see an import still naming the old
.jsfile. Node runs the slice here only because it has no page. On the theater’s site, Vite bundles it for the browser. Either way, the types are stripped, and plain JavaScript runs. In Node’s REPL, both modules load, and two adult tickets come to5100cents, booking fee included.
Final Code
The ticketing slice’s tsconfig.json, with the one new line: