{"record":{"id":"8a313f1f53ea7b23","repo":"discordjs/discord.js","slug":"bitfieldinvalid-json-stringify-bit","errorCode":null,"errorMessage":"BitFieldInvalid: ${JSON.stringify(bit)}","messagePattern":"BitFieldInvalid: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/structures/src/bitfields/BitField.ts","lineNumber":201,"sourceCode":"\t *\n\t * @param bit - bit(s) to resolve\n\t * @returns the numeric value of the bit fields\n\t */\n\tpublic static resolve<Flags extends string = string>(bit: BitFieldResolvable<Flags>): bigint {\n\t\tconst DefaultBit = this.DefaultBit;\n\t\tif (typeof bit === 'bigint' && bit >= DefaultBit) return bit;\n\t\tif (typeof bit === 'number' && BigInt(bit) >= DefaultBit) return BigInt(bit);\n\t\tif (bit instanceof BitField) return bit.bitField;\n\t\tif (Array.isArray(bit)) {\n\t\t\treturn bit.map((bit_) => this.resolve(bit_)).reduce((prev, bit_) => prev | bit_, DefaultBit);\n\t\t}\n\n\t\tif (typeof bit === 'string') {\n\t\t\tif (!Number.isNaN(Number(bit))) return BigInt(bit);\n\t\t\tif (bit in this.Flags) return this.Flags[bit as keyof typeof this.Flags];\n\t\t}\n\n\t\tthrow new Error(`BitFieldInvalid: ${JSON.stringify(bit)}`);\n\t}\n}\n","sourceCodeStart":183,"sourceCodeEnd":204,"githubUrl":"https://github.com/discordjs/discord.js/blob/a81ed8a306d37fdc746e26a634b6a42164ba2c8c/packages/structures/src/bitfields/BitField.ts#L183-L204","documentation":"BitField.resolve() normalizes strings, numbers, and BigInts into a BigInt; if the input is a string that is not numeric and not a known flag name (or is another unsupported type), it throws Error('BitFieldInvalid: <json>').","triggerScenarios":"new Permissions('SendMesage') (typo in flag name), passing an arbitrary string like 'read messages', or passing an object/array to a BitField constructor/any()/add()/remove().","commonSituations":"Flag-name typos, camelCase vs PascalCase confusion ('sendMessages' vs 'SendMessages'), user-supplied strings from config or CLI, or version changes renaming flags.","solutions":["Use a valid flag key exactly as defined in BitField.Flags (verify with Object.keys(Flags))","Pass a numeric or BigInt value instead of a string if you have the raw bits","Validate the string with `bit in Flags` (or Number.isNaN check for numeric strings) before resolving"],"exampleFix":"// before\nconst perms = new Permissions('administrator');\n// after\nconst perms = new Permissions('Administrator'); // exact flag name\n// or guard:\nif (!(name in Permissions.Flags)) throw new Error(`Unknown flag: ${name}`);","handlingStrategy":"validation","validationCode":"const FLAGS = Permissions.Flags;\nfunction isKnownFlag(s: string): boolean {\n  return /^\\d+$/.test(s) || s in FLAGS;\n}","typeGuard":"function isResolvableBit(b: unknown, flags: Record<string, number | bigint>): b is string | number | bigint {\n  if (typeof b === 'number' || typeof b === 'bigint') return true;\n  if (typeof b === 'string') return !Number.isNaN(Number(b)) || b in flags;\n  return false;\n}","tryCatchPattern":"try {\n  const perms = new Permissions(userInput);\n} catch (err) {\n  if (err.message.startsWith('BitFieldInvalid:')) {\n    console.error(`Unknown flag/bits: ${userInput}`);\n  }\n}","preventionTips":["Validate user/config strings against Object.keys(Flags) before constructing","Use exact flag names (case-sensitive) from the Flags map","Prefer numeric/BigInt bit values when the source is not a trusted flag name"],"tags":["validation","bitfield","invalid-argument"],"backgroundTag":"invalid-enum-value","analyzedSha":"a81ed8a306d37fdc746e26a634b6a42164ba2c8c","analyzedAt":"2026-08-30T04:07:22.193Z","schemaVersion":2},"datasetVersion":"2026-08-30T08:17:16.595Z"}