Your repair tickets and parts now have types, and the checker holds every object to them. That makes its messages longer. When a whole list of parts doesn’t fit, npm run check prints a short stack of lines, each indented a little further than the one above.
In this lesson, you’ll read one of those messages from the top and say in plain words what doesn’t fit and why. Then you’ll decide where to fix it, try the fix the message seems to ask for, and find the line that really needs changing. Start with a parts total that came out very wrong.
Read the Error From the Top
A long error is a chain of reasons, and reading it in order tells you what the checker found. The code for Dee’s quote adds up the parts on a job before it adds labor. Last week, the brake parts’ prices were entered from the supplier’s emailed price list, and the brake job’s quote came out at a price no customer would pay. Replace the contents of parts.ts in your repair-queue project folder with the shop’s code:
parts.ts
1type Part = {
2 name: string;
3 priceCents: number;
4};
5
6const brakeJob = [
7 { name: "Brake pads", priceCents: 1899 },
8 { name: "Brake cable", priceCents: "649" },
9];
10const flatJob = [
11 { name: "Inner tube", priceCents: 799 },
12 { name: "Tire levers", priceCents: 499 },
13];
14
15function partsTotal(parts: Part[]) {
16 return parts.reduce(
17 (sum, part) => sum + part.priceCents,
18 0,
19 );
20}
21
22console.log("Brake job parts:", partsTotal(brakeJob));
23console.log("Flat job parts:", partsTotal(flatJob));
Before you hunt through the code for what went wrong, ask the checker what it finds (long lines wrapped for display):
$ npm run check
> [email protected] check
> tsc --noEmit
parts.ts:22:44 - error TS2345: Argument of type
'({ name: string; priceCents: number; } |
{ name: string; priceCents: string; })[]'
is not assignable to parameter of type 'Part[]'.
Type '{ name: string; priceCents: number; } |
{ name: string; priceCents: string; }' is not assignable to type 'Part'.
Type '{ name: string; priceCents: string; }' is not assignable to type 'Part'.
Types of property 'priceCents' are incompatible.
Type 'string' is not assignable to type 'number'.
22 console.log("Brake job parts:", partsTotal(brakeJob));
~~~~~~~~
Found 1 error in parts.ts:22
The first line gives the place and the error code. Line 22, column 44, is where brakeJob is passed to partsTotal(), and the code is TS2345. Then comes the outermost reason, which says where the checker found the mismatch. The argument’s type doesn’t fit the parameter’s type, Part[].
The argument’s type is long, but you can take it apart. The | means or, as in string | number, so the argument is an array whose items have one of two shapes, one with a number price and one with a string price.
Each indented line below explains the one above it and zooms in. An item of the array doesn’t fit Part. Then the item with the string price doesn’t fit Part. Then their priceCents properties disagree. The last line is the innermost reason: a string isn’t a number.