API reference@evolu/commonTypes › Refinement

type Refinement<A, B> = (a: A) => a is B;

Defined in: packages/common/src/Types.ts:134

A type guard function that refines type A to a narrower type B.

Narrowing a value

import { assert, assertEqual, type Refinement } from "@evolu/common";

interface Animal {
  readonly name: string;
}
interface Dog extends Animal {
  readonly breed: string;
}

const isDog: Refinement<Animal, Dog> = (animal): animal is Dog =>
  "breed" in animal;
const dog: Dog = { name: "Dog", breed: "Beagle" };
const animal: Animal = dog;
assert(isDog(animal), "Expected a dog.");

assertEqual(animal.breed, "Beagle");