{"record":{"id":"33918fc4538c8cce","repo":"denoland/deno","slug":"value-must-be-a-bigint-received-typeof-value","errorCode":null,"errorMessage":"Value must be a bigint: received ${typeof value}","messagePattern":"Value must be a bigint: received (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"ext/kv/01_db.ts","lineNumber":732,"sourceCode":"    }\n\n    if (operations.length === 0) {\n      return \"AtomicOperation (empty)\";\n    }\n\n    return `AtomicOperation\\n${ArrayPrototypeJoin(operations, \"\\n\")}`;\n  }\n}\n\nconst MIN_U64 = BigInt(\"0\");\nconst MAX_U64 = BigInt(\"0xffffffffffffffff\");\n\nclass KvU64 {\n  value: bigint;\n\n  constructor(value: bigint) {\n    if (typeof value !== \"bigint\") {\n      throw new TypeError(`Value must be a bigint: received ${typeof value}`);\n    }\n    if (value < MIN_U64) {\n      throw new RangeError(\n        `Value must be a positive bigint: received ${value}`,\n      );\n    }\n    if (value > MAX_U64) {\n      throw new RangeError(\"Value must fit in a 64-bit unsigned integer\");\n    }\n    this.value = value;\n    ObjectFreeze(this);\n  }\n\n  valueOf() {\n    return this.value;\n  }\n\n  toString() {","sourceCodeStart":714,"sourceCodeEnd":750,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/kv/01_db.ts#L714-L750","documentation":"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>`).","triggerScenarios":"`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.","commonSituations":"Counter increments written as plain numbers because that reads naturally; JSON config feeding amounts; porting code from APIs that accept both number and bigint.","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."],"exampleFix":"// before\nawait kv.atomic().sum([\"counters\", \"hits\"], 1).commit(); // Value must be a bigint: received number\n\n// after\nawait kv.atomic().sum([\"counters\", \"hits\"], 1n).commit();","handlingStrategy":"type-guard","validationCode":"const amount = typeof input === \"bigint\" ? input : BigInt(input); // numbers/numeric strings -> bigint\nawait kv.atomic().sum(key, amount).commit();","typeGuard":"const isBigint = (v: unknown): v is bigint => typeof v === \"bigint\";\n\nconst toKvU64Input = (v: number | bigint | string): bigint => BigInt(v);","tryCatchPattern":null,"preventionTips":["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"],"tags":["deno-kv","bigint","u64","kv-sum","typeerror"],"backgroundTag":"bigint-type-mismatch","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","contentChangedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}