Function.invariant
function invariant<T>(value: T | null | undefined, context: string): NonNullable<T>;
Asserts that a value is neither null nor undefined, narrows its type,
and throws an SdkError otherwise.
Used at boundaries where the type system cannot prove non-nullability but runtime invariants guarantee it (e.g. GraphQL fields declared nullable in the generated schema but always populated by the indexer).
Type Parameters
| Type Parameter |
|---|
T |
Parameters
| Parameter | Type | Description |
|---|---|---|
value | T | null | undefined | The value to check. |
context | string | Short identifier of the call site, included in the error. |
Returns
NonNullable<T>
The narrowed value when it is defined.
Throws
SdkErrorTypes.Unexpected when the value is nullish.
Remarks
When called inside an async callback (e.g. Apollo onData subscription
handler), a thrown SdkError is surfaced via the callback's own error
channel — it does NOT propagate to the surrounding hook's ApiResult
tuple. Consumers relying on hook-level error state must not assume
invariant violations inside subscription callbacks will appear there.
Example
const entity = invariant(data?.sub?.entity, "useKeybanAccountBalance subscription");