When does a generic help in designing a function?
Answer out loud or in your head first, then compare.
Answer
A generic is useful when it preserves a known relationship between input and result. If a function processes an array of users, I still want to know what type results from the processing. Then the editor and compiler can check the code that follows without guessing.
For example, mapLimited<T, R> can take items of type T and a function that turns them into R, then return R results. That's clearer than handing everything back as unknown and checking it again. A generic shouldn't pretend to validate network data, though. If I pick the type of an API result arbitrarily, I also need a parser that confirms it.
Why it matters: A practical extension of B-P13, without learning complicated type tricks.
Possible follow-ups: When is a plain, concrete type simpler and better?
How did it go? Be honest — “Not yet” puts the question on your review list.
Start with
- B-P01 · What is Node.js, and how is it different from JavaScript in the browser? — JavaScript, TypeScript and Node.js
- B-P04 · What is a Promise? — JavaScript, TypeScript and Node.js
- B-P10 · What does TypeScript give you, and what doesn't it? — JavaScript, TypeScript and Node.js
Further reading
- TypeScript Handbook: Narrowing(opens in a new tab)Docs; accessed 22.10.2026. Narrowing, discriminated unions and exhaustiveness.