{"record":{"id":"72e15fb0b8b78ba1","repo":"karatelabs/karate","slug":"invalid-hint-to-symbol-toprimitive","errorCode":null,"errorMessage":"invalid hint to Symbol.toPrimitive: ","messagePattern":"invalid hint to Symbol\\.toPrimitive: ","errorType":"exception","errorClass":"JsErrorException","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/JsDatePrototype.java","lineNumber":179,"sourceCode":"    }\n\n    /**\n     * Spec §21.4.4.45 Date.prototype[@@toPrimitive](hint).\n     * - \"string\" / \"default\" → OrdinaryToPrimitive(this, \"string\")\n     * - \"number\"             → OrdinaryToPrimitive(this, \"number\")\n     * - anything else        → TypeError\n     * The \"default\" → \"string\" override is the whole point of Date having its own\n     * @@toPrimitive: it makes `new Date() + \"\"` string-concat instead of timestamp-add.\n     */\n    private Object toPrimitive(Context context, Object[] args) {\n        Object hint = args.length > 0 ? args[0] : Terms.UNDEFINED;\n        String h;\n        if (\"string\".equals(hint) || \"default\".equals(hint)) {\n            h = \"string\";\n        } else if (\"number\".equals(hint)) {\n            h = \"number\";\n        } else {\n            throw JsErrorException.typeError(\"invalid hint to Symbol.toPrimitive: \" + hint);\n        }\n        Object thisObj = context.getThisObject();\n        if (!(thisObj instanceof ObjectLike ol)) {\n            throw JsErrorException.typeError(\"Date.prototype[@@toPrimitive] called on non-object\");\n        }\n        return Terms.ordinaryToPrimitive(ol, h, (CoreContext) context);\n    }\n\n    private Object toISOString(Context context, Object[] args) {\n        JsDate d = requireDate(context);\n        if (d.isInvalid()) {\n            throw JsErrorException.rangeError(\"Invalid time value\");\n        }\n        return formatIso(d);\n    }\n\n    private Object toUTCString(Context context, Object[] args) {\n        JsDate d = requireDate(context);","sourceCodeStart":161,"sourceCodeEnd":197,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/JsDatePrototype.java#L161-L197","documentation":"Date.prototype[Symbol.toPrimitive] accepts only the hints 'string', 'default', or 'number'; any other hint throws this TypeError. Karate's JS engine normalizes 'default' to string-first behavior and rejects everything else before delegating to ordinaryToPrimitive.","triggerScenarios":"Invoking the well-known Symbol.toPrimitive directly with a fabricated hint: d[Symbol.toPrimitive]('custom'), d[Symbol.toPrimitive](''), or generic toPrimitive plumbing passing through an unexpected hint value.","commonSituations":"Custom primitive-coercion utilities written against V8's more forgiving internals; metaprogramming frameworks that synthesize hints; test harnesses probing engine edge cases; typos in hand-rolled coercion code.","solutions":["Use only 'string', 'number', or 'default' as the hint argument","Replace direct Symbol.toPrimitive calls with explicit conversions: String(d), Number(d), d.toString()","Normalize the hint yourself before calling: hint = ['string','number'].includes(hint) ? hint : 'default'","Debug the caller passing the hint and fix it at the source"],"exampleFix":"// before\nlet s = d[Symbol.toPrimitive]('date'); // TypeError\n// after\nlet s = d[Symbol.toPrimitive](hint === 'number' ? 'number' : 'default');","handlingStrategy":"validation","validationCode":"function toPrimitiveHint(d, hint) {\n  const ok = ['string', 'number', 'default'];\n  if (!ok.includes(hint)) throw new TypeError('hint must be string, number, or default');\n  return d[Symbol.toPrimitive](hint);\n}","typeGuard":"const isValidHint = (h) => h === 'string' || h === 'number' || h === 'default';","tryCatchPattern":"let v;\ntry { v = d[Symbol.toPrimitive](hint); } catch (e) {\n  if (e instanceof TypeError && /invalid hint/.test(e.message)) v = String(d);\n  else throw e;\n}","preventionTips":["Only pass 'string', 'number', or 'default' to Symbol.toPrimitive","Prefer explicit String()/Number() conversions over raw Symbol.toPrimitive calls","Normalize synthesized hints in metaprogramming helpers before use","Test custom coercion utilities against all three valid hints"],"tags":["javascript","date","type-error","toprimitive"],"backgroundTag":"invalid-argument-value","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-16T19:17:19.609Z"}