Generic Programming Examples
Generic Programming Examples — purpose and prerequisites
Generic functions preserve relationships between input and output types without using any. The type parameter is inferred from arguments in many cases. Constraints add compile-time guarantees when an operation needs a particular property.
What you’ll learn
- Explain the purpose of generic programming examples.
- Identify the typed contract, runtime responsibilities and common pitfalls.
- Run the example and extend its behavior with a practical exercise.
How the idea works
first infers string for the first call. byId constrains items to have numeric IDs but retains additional properties such as name in its result type.
A generic parameter used only once may add complexity without improving safety; use concrete types when they express the real contract.
Visualize the flow
Run the checked TypeScript example
This source was compiled using TypeScript 5.8.3 with strict checking, then executed with Node.js 22.16.0. Its stdout below is the actual captured output. The core logic is runnable without a framework or a remote API; integration-specific examples appear in a separate lab when needed.
TypeScript source
function first<T>(values: readonly T[]): T | undefined { return values[0]; }
function byId<T extends { id: number }>(items: readonly T[], id: number): T | undefined {
return items.find(item => item.id === id);
}
console.log(first(["a", "b"]));
console.log(byId([{ id: 1, name: "Ada" }, { id: 2, name: "Lin" }], 2));Verified output
a
{ id: 2, name: 'Lin' }Compile: npx tsc --strict --target es2022 --module commonjs generic-programming-examples.ts; run: node generic-programming-examples.js. For examples importing node: modules, install @types/node as a development dependency; for browser or React projects use the separate lab setup.
Trace the execution
first infers string for the first call. byId constrains items to have numeric IDs but retains additional properties such as name in its result type.
Before editing the code, predict what the output will be for one changed input. Run it and compare your prediction with the actual result.
Common mistake and correction
A generic parameter used only once may add complexity without improving safety; use concrete types when they express the real contract.
Determine whether the issue belongs to the type system, a trust boundary, or actual runtime behavior before choosing a correction.
Your turn: extend the example
Write a generic last<T>() function and test string, number, and empty arrays.
Record expected success and failure outcomes, run the code, and compare the observed behavior. Keep the exercise compatible with strict type checking.
Check your understanding
Key idea to remember
first infers string for the first call. byId constrains items to have numeric IDs but retains additional properties such as name in its result type.
Apply the same reasoning to the practice task, then integrate it into a real app only when the runtime and dependency setup are ready.
Reaching the recap and completing the quizzes are separate milestones.