Find User by Id (or undefined)
The find method returns the first matching element or undefined if none match. Given users: { id: number; name: string }[], locate the user with a given id.
This reinforces returning a precise union type (User | undefined) and writing a clear predicate.
Your Task
- Complete findUserById(users: { id: number; name: string }[], id: number): { id: number; name: string } | undefined.
- Return the user if found, otherwise undefined.
Examples
- findUserById([{ id: 1, name: 'Ada' }], 1) → { id: 1, name: 'Ada' }
- findUserById([{ id: 1, name: 'Ada' }], 2) → undefined