> ## Documentation Index
> Fetch the complete documentation index at: https://cosmos-docs-cosmjs-docs.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# @cosmjs/utils

> Assertion and utility helpers

Lightweight utility functions for assertions, type guards, and async helpers used throughout CosmJS.

```bash theme={"system"}
npm install @cosmjs/utils
```

## Assertions

| Function                  | Parameters                                      | Returns                                         |
| ------------------------- | ----------------------------------------------- | ----------------------------------------------- |
| `assert`                  | `condition: any`, `msg?: string`                | `asserts condition` (throws if falsy)           |
| `assertDefined`           | `value: T \| undefined`, `msg?: string`         | `asserts value is T` (throws if undefined)      |
| `assertDefinedAndNotNull` | `value: T \| undefined \| null`, `msg?: string` | `asserts 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.

```typescript theme={"system"}
import { assert, assertDefined } from "@cosmjs/utils";

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

## Type Guards

| Function          | Parameters              | Returns                                |
| ----------------- | ----------------------- | -------------------------------------- |
| `isDefined`       | `value: T \| undefined` | `value is T`                           |
| `isNonNullObject` | `data: unknown`         | `data is object` (also matches arrays) |
| `isUint8Array`    | `value: unknown`        | `value is Uint8Array`                  |

```typescript theme={"system"}
import { isDefined, isNonNullObject } from "@cosmjs/utils";

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

## Async Helpers

| Function | Parameters   | Returns         |
| -------- | ------------ | --------------- |
| `sleep`  | `ms: number` | `Promise<void>` |

```typescript theme={"system"}
import { sleep } from "@cosmjs/utils";

await sleep(1000); // wait 1 second
```

## Array Utilities

| Function                    | Parameters                                                                         | Returns   |
| --------------------------- | ---------------------------------------------------------------------------------- | --------- |
| `arrayContentEquals<T>`     | `a: ArrayLike<T>`, `b: ArrayLike<T>` where `T extends string \| number \| boolean` | `boolean` |
| `arrayContentStartsWith<T>` | `a: ArrayLike<T>`, `b: ArrayLike<T>` where `T extends string \| number \| boolean` | `boolean` |

Elements are compared with strict equality, so these helpers only support arrays of primitives (string/number/boolean).
