is

Declares a user-defined type guard via a type predicate return type.

Since TS 1.6 Spec ↗

Syntax

function fn(x: unknown): x is Type

Examples

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

function use(v: unknown) {
  if (isString(v)) v.toUpperCase();
}
const isDefined = <T>(v: T | undefined): v is T =>
  v !== undefined;

Notes

A predicate `param is Type` tells the compiler that when the function returns true, the argument can be narrowed to `Type`. The function body must actually validate the condition; TypeScript trusts the predicate without checking it.

See also