interface User {
id: number;
name: string;
email: string;
}
const users: User[] = [
{ id: 1, name: 'Alice', email: '[email protected]' },
{ id: 2, name: 'Bob', email: '[email protected]' },
{ id: 3, name: 'Charlie', email: '[email protected]' },
];
function getUserById(id: number): User | undefined {
return users.find(user => user.id === id);
}
const user = getUserById(2);
if (user) {
console.log(user.name);
} else {
console.log('User not found');
}