API reference › @evolu/common › Type › UInt32

type UInt32 = number &
  Brand<"NonNaN"> &
  Brand<"Finite"> &
  Brand<"Int"> &
  Brand<"NonNegative"> &
  Brand<"LessThanOrEqualTo4294967295"> &
  Brand<"UInt32">;

Defined in: packages/common/src/Type.ts:12373

Unsigned 32-bit NonNegativeInt.

Accepts integers from 0 through 4294967295, inclusive, the range of unsigned 32-bit integers in Protocol Buffers and of the JavaScript >>> operator. Like NonNegativeInt, it accepts -0. UInt64 is the unsigned 64-bit bigint counterpart.

Example

import {
  assertEqual,
  assertErr,
  assertOk,
  NonNegativeInt,
  UInt32,
} from "@evolu/common";

assertOk(UInt32.fromUnknown(0), 0);
assertOk(UInt32.fromUnknown(4294967295), 4294967295);
assertErr(UInt32.fromUnknown(-1), { type: "NonNegative", value: -1 });

const tooLarge = UInt32.fromUnknown(4294967296);
assertErr(tooLarge, {
  type: "LessThanOrEqualTo4294967295",
  value: 4294967296,
  max: 4294967295,
});
assertEqual(
  UInt32.formatError(tooLarge.error),
  "The value 4294967296 must be less than or equal to 4294967295.",
);

// A UInt32 is a NonNegativeInt.
const count: NonNegativeInt = UInt32.orThrow(42);
assertEqual(count, 42);