Dev Helpers
← Back

Recognize an object with a numeric ID

Recognize an object with a numeric ID using TypeScript.

Open this example in the interactive library

Source code

const samples: unknown[] = [null, "Ada", 12, { id: 1 }, [2, 3], true];
console.log(
  samples.filter(
    (v): v is { id: number } =>
      typeof v === "object" &&
      v !== null &&
      "id" in v &&
      typeof v.id === "number",
  ),
);

Sample output

[ { id: 1 } ]