Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Migrating One Module at a Time

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.js and ticket-types.js both import from prices.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 .ts file 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.js already 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 are any, so the basket looks finished while its calls go unchecked until someone types that module.
  • Turn JSDoc into annotations. In a .ts file, the checker ignores JSDoc types, so after the rename, lineTotal() would take a string again without an error. Move each @param type onto its parameter and each @returns type 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 .js import such as "./prices.js" to the .ts file 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": true lets an import name a .ts file. It works only in a config that compiles nothing to JavaScript, as "noEmit": true says here. Node and Vite run the files as they are.
  • Type the boundary first. returns-waitlist.js has 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() reads paidCents and seats from a booking, and bookings at the box office also carry a reference and a name, so Booking lists all four. offerToWaitlist() returns an Offer, a waitlist entry plus the seats offered, or null when nobody waiting fits. With strict off, the checker doesn’t hold callers to that null yet.
  • Inside the functions, the checker infers the rest. waitlist holds WaitlistEntry objects, so entry in the callback is one too.
  • JavaScript and TypeScript can sit side by side. ticket-types.js stays JavaScript for now. It imports lineTotal() from prices.ts, and checkJs checks its call against the new annotations.
  • Run the code after every step, too. The check can’t see an import still naming the old .js file. 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 to 5100 cents, booking fee included.

Final Code

The ticketing slice’s tsconfig.json, with the one new line:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 In the last lesson, the checker read the ticketing slice as JavaScript and found a price passed as a string. Now you’ll move the slice to TypeScript, one file per step, keeping it working after each one.

00:13 There are three files to rename. Which one goes first?

00:19 Draw each import as an arrow, from the file that imports to the file it imports from. In returns-waitlist.js, the first line imports the booking fee from the prices module.

00:30 ticket-types.js imports lineTotal from the same module. The prices module imports nothing from the project, so no arrow leaves it. A module at the end of the arrows like this is a leaf of the import graph.

00:47 A file’s types depend on what it imports, never on what imports it. So start at a leaf, and work inward, against the arrows. Without loops in the graph, every file you rename imports only TypeScript.

01:00 Two modules that import each other get renamed together.

01:05 In this small slice, prices.js already has JSDoc, so another order would pass too. In the full ticketing pages, say the basket imports a module whose parameters have no types and nothing to infer them from.

01:18 They’re any. The basket looks finished, but its calls go unchecked until someone types that module.

01:26 Start with the leaf. Rename prices.js, giving it the .ts extension. In a .ts file, the checker reads annotations and ignores JSDoc types. The comments still say number, but a string would get through to lineTotal again.

01:44 So move each type into the code: each parameter’s type becomes an annotation on it, and the return type moves after the parentheses. Then delete the comments.

01:56 Two files still import the old name. The check won’t catch that, because it maps a .js import to the .ts file that would compile to it. But Node and the browser load the exact file named, so change both imports by hand.

02:11 To import the .ts name, add allowImportingTsExtensions. It works only in a config that compiles nothing, as noEmit says here. Node and Vite run the files as they are.

02:25 Run the check, and it passes.

02:29 The returns and waitlist module is next in from the leaf. Rename it. It has no JSDoc yet, so its types go straight into the code, starting with its boundary, the exported functions that the ticketing pages call.

02:42 Every call is checked against their annotations. The refund function reads a booking’s amount paid and seats. Box office bookings also carry a reference and a name, so the Booking type lists all four.

02:55 A WaitlistEntry has a name and a party size.

03:00 Annotate each parameter, and each function’s return type. The waitlist function returns an Offer, a waitlist entry plus the seats offered, or null when nobody waiting fits.

03:12 With strict off, the checker doesn’t hold callers to that null yet. The checker infers the rest. The waitlist holds WaitlistEntry objects, so the entry in the callback is one too.

03:25 Run the check again, and it passes.

03:29 The third file, ticket-types.js, stays JavaScript for now. You’ll migrate it yourself later in this section. It imports lineTotal from the TypeScript file, and checkJs checks its call against the new annotations.

03:44 The check can’t see an import still naming the old .js file, so each step needs a run too. Node runs the slice here only because it has no page. On the theater’s site, Vite bundles it for the browser.

03:57 Either way, the types are stripped, and plain JavaScript runs. Start Node’s REPL in the ticketing folder, and import both modules. They load. Ask for two adult tickets: 5,100 cents, booking fee included.

04:15 So rename one module per step, from the leaves. Annotate its boundary, then run the check and the code.

04:23 Strict is still off. Next, in Tightening Strict Flag by Flag, you’ll turn it back on, one flag at a time.

Become a Member to join the conversation.