Spot the bug in the Invoice predicate

from Type narrowing
TypeScript 6 intermediate 5 min 2 issues to find

Find why this predicate can approve a value that crashes in formatInvoice.

TypeScript
type Invoice = { id: string; total: number };

function isInvoice(value: unknown): value is Invoice {
  return (
    typeof value === "object" &&
    value !== null &&
    "id" in value
  );
}

function formatInvoice(value: unknown): string {
  if (!isInvoice(value)) return "invalid";
  return `${value.id}:${value.total.toFixed(2)}`;
}
Open in playground
Report an error