Homework
Homework — the payoff of strictness
Homework — the payoff of strictness.
- Turn on the flags. In
tsconfig.jsonmake surestrict,noUncheckedIndexedAccess,exactOptionalPropertyTypesare present. Runnpm run typecheck— you'll probably get new errors. That's good: they surface assumptions. noUncheckedIndexedAccess. Find anarr[0]use (e.g. "the newest picture"). Handle theundefinedcase. Prove: without a check — error; with one — clean.assertNever. In aswitchoverLoadStatus(orSortKey,Route), addassertNeverin thedefault. Then add a new union variant and watchnpm run typecheckpoint at every unhandledswitch.exactOptionalPropertyTypes. Try assigning{ hdurl: undefined }wherehdurl?: string. With the flag — error. Understand the difference: "field absent" ≠ "field with undefined".- Generic. Confirm
debouncepreserves argument types: the function returned bydebounce((n: number) => ...)should accept anumber, notany.
Required tests:
- Index test:
arr[0]without a check doesn't compile. - Exhaustiveness test: a new union variant breaks every
switchyou didn't update.
To submit: screenshots: (1) the noUncheckedIndexedAccess error and fix, (2) assertNever broken by adding a variant (pointing at every switch).
Next — Part 4. The app is typed and strict. In lesson 19 we ship it: a Vite production build, environment variables and deploy. And the key lesson about keys:
VITE_NASA_API_KEYends up in the bundle — anyone can read it. When that's fine, and when it's dangerous.