You’ve annotated functions, kept any out with unknown, typed arrays and objects, traced errors to their source, and let inference work where it can. Now you’ll use all of it on the code the course started from: Dee’s quote script, whose quotes have come out wrong.
You’ll set up Dee’s project on your own machine, move her JavaScript to TypeScript, and work through what the checker finds until every quote matches the one Dee works out by hand. This page is the brief and the plan, so keep it open while you build.
The Brief
Dee prices every repair at Spoke & Sprocket with a short script. It adds up the parts in cents, prices the labor in hours at the shop’s hourly rate, and applies a loyalty discount for returning customers. The discount only applies to repairs of $20 or more, and a promotion can add a few percentage points on top when one’s running. None is running right now.
Here are four quotes from this week, worked out by hand on the shop’s calculator:
| Customer | Repair | Returning? | Dee’s quote |
|---|---|---|---|
| Amara | Brake pads, brake cable, 0.75 hours | No | $59.23 |
| Luis | Tire, inner tube, 0.5 hours | No | $65.48 |
| Mei | Chain, 0.5 hours | Yes | $42.74 |
| Kofi | No parts, 0.25 hours | Yes | $11.25 |
The script doesn’t agree with her. Your job is to move it to TypeScript and let the checker show you why. You’re done when both of these hold:
npm run checkreports no errors.node quote.tsprints the four quotes in the table above.
Follow the course’s habits on the way. Read each error before you change anything, and fix the wrong value where it starts, not where it’s reported. Don’t silence the checker with any, as, or @ts-ignore. Annotate parameters, exported functions’ return types, the parts lists, and anything that starts empty, and let inference handle the rest.
Step 1: Set Up Dee’s Project
Your first step is the setup you did for the repair queue, this time on your own. Each command below is one you’ve run before.
Create the project folder, give it a package.json, and install TypeScript as a dev dependency:
$ mkdir spoke-quotes
$ cd spoke-quotes
$ npm init -y
$ npm install --save-dev typescript