Hendrik Lammers

User-defined type guards

To create a user-defined type guard we can create a function that returns a type predicate in the form of parameterName is Type instead of a boolean.

function isCat(animal: Animal): animal is Cat {
  return (animal as Cat).purr !== undefined
}

See the TypeScript docs for more info