Type guards

A type guard is a function or expression that narrows a type. A function with a type predicate, of the form x is T, tells the compiler that the value is of type T when it returns true. It is used to validate unknown data, such as an API response.

Example

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

Related concepts

The whole glossary