Dev Helpers
← Back

Return a nullable lookup result

Return a nullable lookup result using TypeScript.

Open this example in the interactive library

Source code

type Profile = {
  name: string;
  contact?: { email?: string };
  score: number | null;
};
const profiles: Profile[] = [
  { name: "Ada", score: 0 },
  { name: "Ben", contact: { email: "b@example.org" }, score: null },
];
console.log(
  ((name: string): Profile | null =>
    profiles.find((p) => p.name === name) ?? null)("Missing"),
);

Sample output

null