New: Cookbooks and AI ExplanationsStep-by-Step recipes to solve problems connected to Roadmaps and Cheat Sheets. Need more details? Use AI buttons for structured and simple explanations with concrete examples throughout the whole platform.Take a look
I read a setting back from localStorage and TypeScript just calls it any, so nothing checks it anymore.
What you'll have at the end
A settings loader that reads the JSON blob back from localStorage, types the parsed value as unknown, and narrows it with a written guard, returning defaults instead of a crash on bad or stale data.
You need
A JavaScript object you already save to localStorage as a JSON string under a key you control, and a TypeScript project with strict type checking already turned on.
Not covered
Validating JSON that arrives over a network response rather than your own app's local storage, and reaching for a schema-validation library instead of the hand-written guard this recipe builds.
Leans on
Giving a fetch response a real type instead of any
For when the unknown JSON comes back from a network call instead of your own app's storage, a different source with its own failure shapes.
Model an API response so success and failure can't be confused
For when the value you're narrowing already splits into a success or failure case, rather than one settings shape to accept or reject.
Checked 22 Aug 2026
Part of the TypeScript cookbook