Spot the bug in the Profile guard

from Type guards
TypeScript 6 intermediate 4 min 2 issues to find

Find why this guard can approve a value that later crashes.

TypeScript
type Profile = {
  name: string;
  roles: string[];
};

function isProfile(value: unknown): value is Profile {
  return (
    typeof value === "object" &&
    value !== null &&
    "name" in value
  );
}

function roleCount(value: unknown): number {
  if (!isProfile(value)) return 0;
  return value.roles.length;
}
Open in playground
Report an error