TypeScript 7 · Utility types and narrowing

Narrow an unknown value

Check the type before using a value of type unknown, such as parsed JSON.

function parse(input: unknown) {
  if (typeof input === "object" && input !== null && "id" in input) {
    return input.id;
  }
}

Official TypeScript 7 documentation

More utility types and narrowing commands

All utility types and narrowing commands · TypeScript 7 cheatsheet

Was this page helpful?