← All tips

TypeScript: type predicates for custom guards

typescripttyping

Custom guards improve narrowing.

function isString(x: unknown): x is string {
  return typeof x === 'string';
}