denoland/deno · error · RangeError

Value must be a positive bigint: received

Error message

Value must be a positive bigint: received ${value}

What it means

KvU64 values are unsigned by definition (MIN_U64 = 0n in the source). A bigint below 0 throws RangeError(`Value must be a positive bigint: received <value>`). The u64 counter domain is add-only: there is no negative accumulator, so a decrement that would cross zero is a domain error by design, not a wrapped value.

Solutions

  1. Validate the delta first: if (delta < 0n) handle the negative case explicitly (clamp, floor at 0n, or raise a domain error).
  2. For signed state, read the current value and set() the new signed number instead of using u64 sum(); or store magnitude and sign separately.
  3. Never assume wrap-around: u64 semantics reject rather than underflow.

Example fix

// before
await kv.atomic().sum(["stock", sku], delta).commit(); // delta < 0n throws

// after
if (delta < 0n) throw new RangeError(`stock underflow for ${sku}`);
await kv.atomic().sum(["stock", sku], delta).commit();
Defensive patterns

Strategy: validation

Validate before calling

function assertU64(v: bigint) {
  if (v < 0n) throw new RangeError(`negative u64 not allowed: ${v}`);
  if (v > 0xffffffffffffffffn) throw new RangeError(`u64 overflow: ${v}`);
}
assertU64(delta);
await kv.atomic().sum(key, delta).commit();

Type guard

const isU64Range = (v: bigint): v is bigint => v >= 0n && v <= 0xffffffffffffffffn;

Prevention

When it happens

Trigger: `.sum(key, -5n)`; `new Deno.KvU64(-1n)`; feeding a computed delta (current - previous) straight to sum() when the result can be negative.

Common situations: Rate/stock/inventory counters that can decrease; porting signed arithmetic from SQL DECIMAL or int columns; diff calculations without a floor at zero.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


AI-assisted analysis of denoland/deno@9ad36f7a2c (2026-08-20). Data as JSON: /api/errors/00607af028a60c9b. Report an issue: GitHub.

Appendix: source

Thrown at ext/kv/01_db.ts:735

      return "AtomicOperation (empty)";
    }

    return `AtomicOperation\n${ArrayPrototypeJoin(operations, "\n")}`;
  }
}

const MIN_U64 = BigInt("0");
const MAX_U64 = BigInt("0xffffffffffffffff");

class KvU64 {
  value: bigint;

  constructor(value: bigint) {
    if (typeof value !== "bigint") {
      throw new TypeError(`Value must be a bigint: received ${typeof value}`);
    }
    if (value < MIN_U64) {
      throw new RangeError(
        `Value must be a positive bigint: received ${value}`,
      );
    }
    if (value > MAX_U64) {
      throw new RangeError("Value must fit in a 64-bit unsigned integer");
    }
    this.value = value;
    ObjectFreeze(this);
  }

  valueOf() {
    return this.value;
  }

  toString() {
    return BigIntPrototypeToString(this.value);
  }

View on GitHub (pinned to 9ad36f7a2c)