{"record":{"id":"a3206641a77eb163","repo":"discordjs/discord.js","slug":"cannot-convert-bitfield-value-this-bitfield-to","errorCode":null,"errorMessage":"Cannot convert bitfield value ${this.bitField} to number, as it is bigger than ${Number.MAX_SAFE_INTEGER} (the maximum safe integer)","messagePattern":"Cannot convert bitfield value (.+?) to number, as it is bigger than (.+?) \\(the maximum safe integer\\)","errorType":"validation","errorClass":"RangeError","httpStatus":null,"severity":"error","filePath":"packages/structures/src/bitfields/BitField.ts","lineNumber":160,"sourceCode":"\t\t}\n\n\t\treturn serialized;\n\t}\n\n\t/**\n\t * Gets an Array of bit field names based on the bits available.\n\t *\n\t * @param hasParams - Additional parameters for the has method, if any\n\t * @returns An Array of bit field names\n\t */\n\tpublic toArray(...hasParams: readonly unknown[]) {\n\t\treturn [...this[Symbol.iterator](...hasParams)];\n\t}\n\n\tpublic toJSON(asNumber?: boolean) {\n\t\tif (asNumber) {\n\t\t\tif (this.bitField > Number.MAX_SAFE_INTEGER) {\n\t\t\t\tthrow new RangeError(\n\t\t\t\t\t`Cannot convert bitfield value ${this.bitField} to number, as it is bigger than ${Number.MAX_SAFE_INTEGER} (the maximum safe integer)`,\n\t\t\t\t);\n\t\t\t}\n\n\t\t\treturn Number(this.bitField);\n\t\t}\n\n\t\treturn this.bitField.toString();\n\t}\n\n\tpublic valueOf() {\n\t\treturn this.bitField;\n\t}\n\n\tpublic *[Symbol.iterator](...hasParams: unknown[]) {\n\t\tfor (const bitName of Object.keys(this.constructor.Flags)) {\n\t\t\tif (Number.isNaN(Number(bitName)) && this.has(bitName as Flags, ...hasParams)) yield bitName as Flags;\n\t\t}","sourceCodeStart":142,"sourceCodeEnd":178,"githubUrl":"https://github.com/discordjs/discord.js/blob/a81ed8a306d37fdc746e26a634b6a42164ba2c8c/packages/structures/src/bitfields/BitField.ts#L142-L178","documentation":"BitField.toJSON(asNumber) converts the bitfield to a number when asNumber is true, but BigInt values exceed Number.MAX_SAFE_INTEGER cannot be represented exactly, so a RangeError is thrown to prevent silent precision loss.","triggerScenarios":"Calling bitField.toJSON(true) (or JSON.stringify with a numeric replacer path) on a bitfield whose value is a BigInt larger than 2^53-1, e.g. large permission or intent combinations.","commonSituations":"Serializing Intents or Permissions bitfields that combine many flags, then requesting numeric output; logging with asNumber for compact output on wide bitfields.","solutions":["Call toJSON() without arguments to get the string/array form instead of a number","Use toString()/BigInt output when the value may exceed MAX_SAFE_INTEGER","Reduce the flags combined if you truly need a safe numeric representation"],"exampleFix":"// before\nconst num = permissions.toJSON(true);\n// after\nconst str = permissions.toJSON(); // string form, safe for big values\nconst num = BigInt(str) <= BigInt(Number.MAX_SAFE_INTEGER) ? Number(str) : str;","handlingStrategy":"validation","validationCode":"if (typeof bf.bitField === 'bigint' && bf.bitField > BigInt(Number.MAX_SAFE_INTEGER)) {\n  // use string serialization instead of toJSON(true)\n}","typeGuard":"function isSafeAsNumber(bf: BitField): boolean {\n  return typeof bf.bitField === 'number' || bf.bitField <= BigInt(Number.MAX_SAFE_INTEGER);\n}","tryCatchPattern":"try {\n  const n = bitfield.toJSON(true);\n} catch (err) {\n  if (err instanceof RangeError && err.message.includes('MAX_SAFE_INTEGER')) {\n    const s = bitfield.toJSON(); // string fallback\n  }\n}","preventionTips":["Default to string serialization for bitfields you do not control","Check value size against MAX_SAFE_INTEGER before numeric conversion","Remember JS numbers lose precision above 2^53-1"],"tags":["bigint","bitfield","serialization"],"backgroundTag":"unsafe-integer-conversion","analyzedSha":"a81ed8a306d37fdc746e26a634b6a42164ba2c8c","analyzedAt":"2026-08-30T04:07:22.193Z","schemaVersion":2},"datasetVersion":"2026-08-30T08:17:16.595Z"}