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": truein 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 overstrict, wherever it sits incompilerOptions, so you can keep"strict": falseand add the flags one by one below it. ticket-types.jscounts too. It’s still JavaScript, butcheckJsputs 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 aDISCOUNTSlist of codes anddiscountedTotal(), 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, isany. A value typedanycan holdnullwithout 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 ofdiscountedTotal(). The fix in a JavaScript file is a JSDoc comment like the ones on Tobias’s functions, using hisTicketType. - Next,
strictNullChecks. Here, its errors said a value is possiblyundefined(TS18048) or possiblynull(TS18047). Read each one before you fix it. .find()givesundefinedwhen no code matches, anddiscountedTotal()read.percentanyway.- 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
percentwasnull. The checker infers the list’s types from its entries, sodiscount.percentwasnumber | null. In the arithmetic,nullbecame 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-errorand a reason. When an error is real and its fix comes later, put a// @ts-expect-errorcomment 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": falseand the two flags with"strict": true. The check still passes, because thestrictfamily’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
nullleft in the list, sodiscount.percentwas a plainnumber. 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-ignorecomment, 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:
- Add
"noImplicitAny": trueunder"strict": false. Runnpm run check, and type every parameter it reports (here, TS7006). - Add
"strictNullChecks": true. Fix each possiblyundefinedornullvalue it reports (here, TS18048 and TS18047), or mark a gap you can’t close yet with// @ts-expect-error: <reason>. - Replace
"strict": falseand the two flags with"strict": true, and run the check again. In this slice, the other strict flags had nothing to report. - When a fix lands later, the check reports the stale comment (TS2578). Delete it.