Checking JavaScript Before Renaming

Door-check is configured and checked, and it was TypeScript from its first line. The Lantern Street Theatre’s ticketing pages are a different job. Tobias Okonkwo wrote them in JavaScript, Mei Chen’s box office sells every ticket through them, and they can’t stop working while someone adds types.

In this section, you’ll migrate a slice of that code to TypeScript in steps that keep it running. In this lesson, you’ll have the checker read the JavaScript as it is, give it types in comments, and fix what it finds before you rename a single file. Start with a question about the volunteer’s setup. What does tsc check in this folder today?

See What allowJs Checks

A config can let JavaScript files into a project without checking them, so the first job is to find out what the checker actually reads. The course’s sample code has a ticketing folder next to door-check. It holds three modules copied out of the ticketing pages so you can work on them on their own:

  • src/prices.js works out the price of a line in the basket and formats it for the page.
  • src/returns-waitlist.js works out refunds for returned tickets and offers the seats to the waitlist.
  • src/ticket-types.js holds the ticket types and their prices, which Tobias annotated with types in comments.

returns-waitlist.js and ticket-types.js import from prices.js. All three run in the browser, so the folder has one config, which Tobias added:

Language: JSON Filename: tsconfig.json
{
  "include": ["src"],
  "compilerOptions": {
    "target": "esnext",
    "lib": ["esnext", "dom"],
    "module": "esnext",
    "moduleResolution": "bundler",
    "allowJs": true,
    "strict": false,
    "noEmit": true,
    "verbatimModuleSyntax": true,
    "erasableSyntaxOnly": true
  }
}

Most of it matches door-check’s page config. Two lines are new. "allowJs" lets .js files into the project, so tsc reads them, and a .ts file that imports one gets its types instead of any. "strict": false turns off the checks that current TypeScript turns on by default. You’ll turn them back on later in this section, once the code is ready for them.

Open a terminal in the folder, install its packages, and run the check:

Language: Shell
$ cd ticketing
$ npm install
$ npm run check

> [email protected] check
> tsc --noEmit -p tsconfig.json

No errors. Like door-check, the project pins TypeScript to a release that typescript-eslint supports. But a passing check doesn’t mean much here. With allowJs alone, tsc reads the JavaScript files but doesn’t report type errors in them.

Open src/prices.js, the module the basket uses for its totals:

Language: JavaScript Filename: src/prices.js
 1export const BOOKING_FEE = 150;
 2
 3export function lineTotal(priceCents, count) {
 4  return (priceCents + BOOKING_FEE) * count;
 5}
 6
 7export function groupLineTotal(priceCents, count) {
 8  const discounted = (priceCents * 0.9).toFixed(0);
 9  return lineTotal(discounted, count);
10}
11
12/**
13 * @param {number} cents
14 * @returns {string}
15 */
16export function formatPrice(cents) {
17  return `$${(cents / 100).toFixed(2)}`;
18}

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

Become a Member to join the conversation.