Complete worked example
typescript
function byId<T extends { id: string }>(items: readonly T[], id: string): T | undefined {
return items.find(item => item.id === id);
}
const found = byId([{ id: "r1", salary: 100 }], "r1");- Reasoning
- The implementation uses only the constraint while the returned existing element preserves additional caller fields.