Skip to main content
Lightweight utility functions for assertions, type guards, and async helpers used throughout CosmJS.
npm install @cosmjs/utils

Assertions

FunctionParametersReturns
assertcondition: any, msg?: stringasserts condition (throws if falsy)
assertDefinedvalue: T | undefined, msg?: stringasserts value is T (throws if undefined)
assertDefinedAndNotNullvalue: T | undefined | null, msg?: stringasserts value is T (throws if null/undefined)
These helpers use TypeScript assertion signatures, so the compiler narrows the type of the argument in the enclosing scope after a successful call.
import { assert, assertDefined } from "@cosmjs/utils";

assert(amount > 0, "Amount must be positive");
assertDefined(account, "Account not found");

Type Guards

FunctionParametersReturns
isDefinedvalue: T | undefinedvalue is T
isNonNullObjectdata: unknowndata is object (also matches arrays)
isUint8Arrayvalue: unknownvalue is Uint8Array
import { isDefined, isNonNullObject } from "@cosmjs/utils";

if (isDefined(optional)) {
  optional.property; // TypeScript knows it's defined
}

Async Helpers

FunctionParametersReturns
sleepms: numberPromise<void>
import { sleep } from "@cosmjs/utils";

await sleep(1000); // wait 1 second

Array Utilities

FunctionParametersReturns
arrayContentEquals<T>a: ArrayLike<T>, b: ArrayLike<T> where T extends string | number | booleanboolean
arrayContentStartsWith<T>a: ArrayLike<T>, b: ArrayLike<T> where T extends string | number | booleanboolean
Elements are compared with strict equality, so these helpers only support arrays of primitives (string/number/boolean).