denoland/deno · error · TypeError
Value must be a bigint: received
Error message
Value must be a bigint: received ${typeof value} What it means
Deno.KvU64 is the 64-bit unsigned counter wrapper used by atomic().sum()/.min()/.max() and serialized as a 'u64' raw value. Its constructor hard-requires a bigint because values above Number.MAX_SAFE_INTEGER cannot round-trip as JS numbers; passing anything else throws TypeError(`Value must be a bigint: received <typeof value>`).
Solutions
- Pass bigint literals: .sum(key, 1n), or convert with BigInt(n).
- Convert at the boundary: const inc = BigInt(input.amount ?? 1).
- Type helper parameters as (n: bigint) so TypeScript rejects numbers at the call site.
Example fix
// before await kv.atomic().sum(["counters", "hits"], 1).commit(); // Value must be a bigint: received number // after await kv.atomic().sum(["counters", "hits"], 1n).commit();
Defensive patterns
Strategy: type-guard
Validate before calling
const amount = typeof input === "bigint" ? input : BigInt(input); // numbers/numeric strings -> bigint await kv.atomic().sum(key, amount).commit();
Type guard
const isBigint = (v: unknown): v is bigint => typeof v === "bigint"; const toKvU64Input = (v: number | bigint | string): bigint => BigInt(v);
Prevention
- Use 1n-style literals for all counter arithmetic
- Convert JSON-parsed amounts to bigint at the boundary with BigInt()
- Type increment helpers as (n: bigint) so numbers fail to compile
When it happens
Trigger: `kv.atomic().sum(key, 1)` with a plain number (sum() internally constructs KvU64 with your argument); `new Deno.KvU64("42")`; increment amounts arriving from JSON.parse (always numbers), query strings, or protobuf-style decoded fields.
Common situations: Counter increments written as plain numbers because that reads naturally; JSON config feeding amounts; porting code from APIs that accept both number and bigint.
Related errors
- Value must be a positive bigint: received
- Value must fit in a 64-bit unsigned integer
- Cannot convert a BigInt value to a number
- Deno.Kv can not be constructed: use Deno.openKv instead
- A chunk may only be either a string or an Uint8Array
AI-assisted analysis of denoland/deno@9ad36f7a2c (2026-08-20).
Data as JSON: /api/errors/33918fc4538c8cce.
Report an issue: GitHub.
Appendix: source
Thrown at ext/kv/01_db.ts:732
}
if (operations.length === 0) {
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() {View on GitHub (pinned to 9ad36f7a2c)