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've now written the same lookup function three separate times, once for every data shape, because TypeScript won't let me share just one version of it.
What you'll have at the end
One generic function replacing three near-identical ones, with the same type safety on each call
You need
Three separate functions, one per data shape, that each loop over an array looking for one item whose named field matches a value you pass in.
Not covered
Replacing the array scan itself with a Map or a database index for faster lookups, and any generic version that returns more than one matching item.
Leans on
Deriving a smaller type from a big interface with Pick
For trimming a large interface down to just the fields a caller needs, the sibling generic pattern to the lookup function built here.
Narrow an unknown JSON value before you trust it
For validating a whole value's shape once it arrives from outside the type system, the same problem the runtime key check here solves for a single key.
Checked 22 Aug 2026
Part of the TypeScript cookbook