Dev Helpers
← Back

Guard a boolean field

Guard a boolean field using TypeScript.

Open this example in the interactive library

Source code

const samples: unknown[] = [null, "Ada", 12, { id: 1 }, [2, 3], true];
console.log(
  ((v: unknown): v is boolean => typeof v === "boolean")(samples.at(-1)),
);

Sample output

true