Open this lesson on a wider screen to play it. The narrated transcript and animated code need room to sit side by side.
← All lessons// Plain TypeScript: Array.prototype.find returns T | undefined. The "| undefined"
// is the half everyone forgets — reach for the value directly and the type lies
// about whether it is there.
interface Account {
id: number;
name: string;
}
const accounts: Account[] = [
{ id: 1, name: "Ada" },
{ id: 2, name: "Linus" },
];
const found = accounts.find((u) => u.id === 3);
// ^? Account | undefined
// found.name would not compile; found!.name would compile and crash at runtime.
console.log(found?.name ?? "no user"); // no user