Homework

Homework — the payoff of strictness

Homework — the payoff of strictness.

  1. Turn on the flags. In tsconfig.json make sure strict, noUncheckedIndexedAccess, exactOptionalPropertyTypes are present. Run npm run typecheck — you'll probably get new errors. That's good: they surface assumptions.
  2. noUncheckedIndexedAccess. Find an arr[0] use (e.g. "the newest picture"). Handle the undefined case. Prove: without a check — error; with one — clean.
  3. assertNever. In a switch over LoadStatus (or SortKey, Route), add assertNever in the default. Then add a new union variant and watch npm run typecheck point at every unhandled switch.
  4. exactOptionalPropertyTypes. Try assigning { hdurl: undefined } where hdurl?: string. With the flag — error. Understand the difference: "field absent" ≠ "field with undefined".
  5. Generic. Confirm debounce preserves argument types: the function returned by debounce((n: number) => ...) should accept a number, not any.

Required tests:

  • Index test: arr[0] without a check doesn't compile.
  • Exhaustiveness test: a new union variant breaks every switch you 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_KEY ends up in the bundle — anyone can read it. When that's fine, and when it's dangerous.