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

Tightening Strict Flag by Flag

In this lesson, you turned strict back on in the ticketing slice one flag at a time, with npm run check passing again before each next flag, and you marked one known gap with @ts-expect-error until the data could close it.

Key Points

  • One flag at a time. Setting "strict": true in one go gives the slice five errors at once. In the full ticketing pages, the list would be far longer, and the check would fail until every one is fixed. A flag you set yourself wins over strict, wherever it sits in compilerOptions, so you can keep "strict": false and add the flags one by one below it.
  • ticket-types.js counts too. It’s still JavaScript, but checkJs puts it under every flag you turn on. Below Tobias’s code, an AI assistant added a discount part that the earlier videos didn’t show. It has a DISCOUNTS list of codes and discountedTotal(), which takes a code’s percent off the total.
  • First, noImplicitAny. A parameter with no type, and nothing for the checker to infer one from, is any. A value typed any can hold null without a single error, so give everything a type before you look for nulls.
  • Here, the flag’s errors were TS7006, Parameter '...' implicitly has an 'any' type., on the three parameters of discountedTotal(). The fix in a JavaScript file is a JSDoc comment like the ones on Tobias’s functions, using his TicketType.
  • Next, strictNullChecks. Here, its errors said a value is possibly undefined (TS18048) or possibly null (TS18047). Read each one before you fix it.
  • .find() gives undefined when no code matches, and discountedTotal() read .percent anyway.
  • In Node’s REPL, a code that isn’t on the list crashed with TypeError: Cannot read properties of undefined (reading 'percent'). The checker found that bug before the code ever ran. Fix it in the code by returning the full total when nothing matches.
  • A gap you can’t close yet. The schools entry had no rate yet, so its percent was null. The checker infers the list’s types from its entries, so discount.percent was number | null. In the arithmetic, null became zero, so a school group paid full price. Only Mei could set the rate, though, so any fix in the code would have been a guess.
  • Mark a known gap with @ts-expect-error and a reason. When an error is real and its fix comes later, put a // @ts-expect-error comment on the line above, with the reason after a colon. It tells the checker to expect an error on the next line and not report it, so the check passes again.
  • Then the whole family. Once the first two flags pass, replace "strict": false and the two flags with "strict": true. The check still passes, because the strict family’s other flags had nothing to report in this slice.
  • A closed gap reports itself. When Mei set the schools rate at 15 percent, there was no null left in the list, so discount.percent was a plain number. The line below the comment had no error left to expect, so the check failed with TS2578, Unused '@ts-expect-error' directive., on the comment itself. Delete it, and the check passes.
  • The older // @ts-ignore comment, which also hides errors, would have stayed silent there. Nothing would ever tell you to delete it, so it would stay in the file for good.
  • Both comments hide every error on the line below, not only the gap you marked. So a passing check doesn’t prove the gap is the only error there. Keep the marked line to the expression with the gap, and delete the comment as soon as the fix lands.

The order, as a reference:

  1. Add "noImplicitAny": true under "strict": false. Run npm run check, and type every parameter it reports (here, TS7006).
  2. Add "strictNullChecks": true. Fix each possibly undefined or null value it reports (here, TS18048 and TS18047), or mark a gap you can’t close yet with // @ts-expect-error: <reason>.
  3. Replace "strict": false and the two flags with "strict": true, and run the check again. In this slice, the other strict flags had nothing to report.
  4. When a fix lands later, the check reports the stale comment (TS2578). Delete it.

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 Two of the ticketing slice’s three files are TypeScript now, and the check passes. But Tobias’s config still sets strict to false, so the checker skips checks that current TypeScript runs by default.

00:12 In this video, you’ll turn them back on. You could set strict to true in one go. In this small slice, that gives five errors at once. In the full ticketing pages, the list would be far longer, and the check would stay red until all were fixed.

00:28 So turn on one flag at a time, and get the check passing again before the next.

00:34 Start with noImplicitAny. A parameter with no type, and nothing to infer one from, is any. And a value typed any can hold null without a single error. So type everything before you look for nulls.

00:49 Add the flag below strict false. A flag you set yourself wins over strict, wherever it sits. Run the check, and you get three errors, each saying a parameter implicitly has an any type.

01:03 They’re all on one line of ticket-types.js. It’s still JavaScript, but checkJs puts it under every flag. Below Tobias’s code, an AI assistant added a discount part the last videos didn’t show: a list of codes, and a function that takes a code’s percent off the total.

01:20 The function’s parameters have no types. Give the function a JSDoc comment like Tobias’s above it. Run the check again, and it passes.

01:31 Next, add strictNullChecks. The check fails again, with two errors on the discount function’s last line. Read the first one: discount is possibly undefined.

01:44 The find method gives undefined when no code matches, and the function reads percent anyway. The slice has no page, so try it in Node’s REPL. Import the module, and ask for two adult tickets with a code that isn’t on the list.

01:59 It crashes, reading percent of undefined.

02:03 The checker found that bug before the code ever ran. Fix the code: when nothing matches, return the full total. Run the check, and one error is left.

02:17 This one says percent is possibly null. The checker infers the list’s types from its entries, and the schools entry has null until Mei sets its rate. In the arithmetic, null becomes zero, so a school group pays full price.

02:31 Only Mei can set it, so any fix in the code would be a guess. That’s a known gap. The error is real, and its fix comes later. Mark it with a ts-expect-error comment on the line above, and put the reason after a colon.

02:47 It tells the checker to expect an error on the next line and not report it. Run the check, and it passes.

02:55 Now replace strict false and the two flags with strict set to true. Run the check, and it still passes. The strict family’s other flags have nothing to report here.

03:08 Later, Mei sets the schools rate at 15 percent. Change null to 15. With null gone from the list, percent is a plain number to the checker. Run the check, and it fails with TS2578: unused ts-expect-error directive.

03:25 The line below has no error left to expect, so now the comment itself is the error.

03:32 Delete it, and the check passes.

03:35 The older ts-ignore comment, which also hides errors, would have stayed silent here, and in the file for good. Both comments hide every error on the line below.

03:47 So keep that line to the gap’s expression, and delete the comment once it’s fixed.

03:54 So turn strict on one flag at a time. After each one, fix every error, or mark a gap you can’t close yet with ts-expect-error and a reason. Get the check passing, then turn on the next flag.

04:08 Next, in Exercise: Migrate the Ticket-Type Helper, you’ll turn the ticket types and their discounts into TypeScript yourself.

Become a Member to join the conversation.