{"record":{"id":"fbd37c7d0e8f05fc","repo":"zloirock/core-js","slug":"cannot-convert-a-symbol-value-to-a-number","errorCode":null,"errorMessage":"Cannot convert a Symbol value to a number","messagePattern":"Cannot convert a Symbol value to a number","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/core-js/modules/es.number.constructor.js","lineNumber":41,"sourceCode":"var PureNumberNamespace = path[NUMBER];\nvar NumberPrototype = NativeNumber.prototype;\nvar TypeError = globalThis.TypeError;\nvar stringSlice = uncurryThis(''.slice);\nvar charCodeAt = uncurryThis(''.charCodeAt);\n\n// `ToNumeric` abstract operation\n// https://tc39.es/ecma262/#sec-tonumeric\nvar toNumeric = function (value) {\n  var primValue = toPrimitive(value, 'number');\n  return typeof primValue == 'bigint' ? primValue : toNumber(primValue);\n};\n\n// `ToNumber` abstract operation\n// https://tc39.es/ecma262/#sec-tonumber\nvar toNumber = function (argument) {\n  var it = toPrimitive(argument, 'number');\n  var first, third, radix, maxCode, digits, length, index, code;\n  if (isSymbol(it)) throw new TypeError('Cannot convert a Symbol value to a number');\n  if (typeof it == 'string' && it.length > 2) {\n    it = trim(it);\n    first = charCodeAt(it, 0);\n    if (first === 43 || first === 45) {\n      third = charCodeAt(it, 2);\n      if (third === 88 || third === 120) return NaN; // Number('+0x1') should be NaN, old V8 fix\n    } else if (first === 48) {\n      switch (charCodeAt(it, 1)) {\n        // fast equal of /^0b[01]+$/i\n        case 66:\n        case 98:\n          radix = 2;\n          maxCode = 49;\n          break;\n        // fast equal of /^0o[0-7]+$/i\n        case 79:\n        case 111:\n          radix = 8;","sourceCodeStart":23,"sourceCodeEnd":59,"githubUrl":"https://github.com/zloirock/core-js/blob/84e45fba098dd3a177d5cf2247d06ab8e98d3790/packages/core-js/modules/es.number.constructor.js#L23-L59","documentation":"core-js's Number constructor implementation calls ToNumber/ToNumeric, which first converts the argument via ToPrimitive. If the result is still a Symbol, the spec forbids numeric conversion, so core-js throws a TypeError instead of silently producing NaN. This polyfill path runs when the environment's native Number coercion is patched or insufficient.","triggerScenarios":"Calling new Number(sym) or Number(sym) where sym is a Symbol; any polyfilled numeric coercion (parseInt, parseFloat, +sym, Math.* via Number paths) receiving a Symbol; BigInt()-like polyfill paths using toNumeric with a Symbol argument.","commonSituations":"Unwrapping a Symbol-keyed value or a library sentinel (e.g. a private-field Symbol from a dependency) and passing it to Number(); arithmetic on mixed values where one operand is a Symbol; deserializing JSON-like data that contained a Symbol after a library upgrade replaced string ids with Symbols.","solutions":["Remove the numeric coercion of the Symbol; use the underlying primitive value instead (store the actual id alongside the Symbol).","Use Symbol.keyFor or the symbol's registered key to look up a numeric value rather than coercing the symbol itself.","Add a type check (typeof v === 'symbol') before calling Number() and handle it explicitly.","Ensure you're not accidentally passing the Symbol object returned by Object.getOwnPropertySymbols instead of the data value.","If relying on polyfill behavior, upgrade core-js — newer versions keep spec-compliant coercion messages and paths."],"exampleFix":"// before\nconst key = getProp(obj); // may be a Symbol\nconst id = Number(key); // TypeError\n// after\nconst key = getProp(obj);\nconst id = typeof key === 'symbol' ? null : Number(key);","handlingStrategy":"type-guard","validationCode":"function assertNotSymbol(v) { if (typeof v === 'symbol') throw new TypeError('Cannot coerce Symbol to number'); }\nassertNotSymbol(value); const n = Number(value);","typeGuard":"function isSymbol(v) { return typeof v === 'symbol'; }\n// usage: if (!isSymbol(v)) { Number(v) }","tryCatchPattern":"try {\n  const n = Number(value);\n} catch (e) {\n  if (e instanceof TypeError && /Symbol/.test(e.message)) {\n    // handle symbol case: use alternate key/lookup\n  } else throw e;\n}","preventionTips":["Never use Symbols as numeric data; keep a parallel primitive id.","Type-check values crossing API boundaries (typeof check before coercion).","Avoid Symbol() sentinels in data structures that get numerically coerced.","Enable lint rules (e.g. no-implicit-coercion patterns) and strict TS types to catch symbol unions."],"tags":["javascript","polyfill","typeerror","symbol"],"backgroundTag":"symbol-to-number-conversion","analyzedSha":"84e45fba098dd3a177d5cf2247d06ab8e98d3790","analyzedAt":"2026-08-30T20:36:10.323Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}