{"record":{"id":"9eafc887116cde34","repo":"karatelabs/karate","slug":"set-like-object-s-size-is-not-a-number","errorCode":null,"errorMessage":"Set-like object's size is not a number","messagePattern":"Set-like object's size is not a number","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/JsSetPrototype.java","lineNumber":167,"sourceCode":"\n        SetRecord(ObjectLike setObj, long intSize, JsCallable has, JsCallable keys) {\n            this.setObj = setObj;\n            this.intSize = intSize;\n            this.has = has;\n            this.keys = keys;\n        }\n    }\n\n    private static SetRecord getSetRecord(Object other, Context context) {\n        if (!(other instanceof ObjectLike obj)) {\n            throw JsErrorException.typeError(\"Set.prototype method called with non-object\");\n        }\n        CoreContext cc = context instanceof CoreContext c ? c : null;\n        Object rawSize = obj.getMember(\"size\", obj, cc);\n        Number numSize = Terms.toNumberCoerce(rawSize, cc);\n        double d = numSize.doubleValue();\n        if (Double.isNaN(d)) {\n            throw JsErrorException.typeError(\"Set-like object's size is not a number\");\n        }\n        // ToIntegerOrInfinity, then RangeError on negative — spec step 7.\n        long intSize;\n        if (Double.isInfinite(d)) {\n            intSize = d > 0 ? Long.MAX_VALUE : Long.MIN_VALUE;\n        } else {\n            intSize = (long) d;\n        }\n        if (intSize < 0) {\n            throw JsErrorException.rangeError(\"Set-like object's size is negative\");\n        }\n        Object hasFn = obj.getMember(\"has\", obj, cc);\n        if (!(hasFn instanceof JsCallable has)) {\n            throw JsErrorException.typeError(\"Set-like object's 'has' is not callable\");\n        }\n        Object keysFn = obj.getMember(\"keys\", obj, cc);\n        if (!(keysFn instanceof JsCallable keys)) {\n            throw JsErrorException.typeError(\"Set-like object's 'keys' is not callable\");","sourceCodeStart":149,"sourceCodeEnd":185,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/JsSetPrototype.java#L149-L185","documentation":"SetRecord validation per the ES set-union/intersection spec: when a Set-like object is passed to a Set.prototype method, its 'size' property must coerce to a number, but NaN was produced. Fix the Set-like object so 'size' is a numeric value.","triggerScenarios":"set.difference({ size: 'not-a-number', has: fn, keys: fn }); or an object whose size getter returns undefined/NaN.","commonSituations":"Hand-rolled Set-like objects with string or undefined size; typo like `lenght`-style mistakes on the custom object; getters that fail and return undefined.","solutions":["Give the Set-like a numeric size member (e.g. size: 3).","Use a real Set as the argument so size is always numeric.","Validate Number.isInteger(other.size) before calling the method."],"exampleFix":"// before\nconst like = { size: 'many', has: k => true, keys: () => [][Symbol.iterator]() };\nmySet.difference(like);\n// after\nconst like = { size: 0, has: k => true, keys: () => [][Symbol.iterator]() };\nmySet.difference(like);","handlingStrategy":"validation","validationCode":"if (!Number.isFinite(Number(other?.size))) throw new TypeError('size must be a number');","typeGuard":"function hasNumericSize(v) { return v !== null && typeof v === 'object' && Number.isFinite(Number(v.size)); }","tryCatchPattern":"try { return mySet.difference(other); } catch (e) { if (String(e).includes(\"size is not a number\")) return mySet.difference(new Set()); throw e; }","preventionTips":["Give custom Set-likes a numeric size.","Avoid string sizes like 'many'.","Prefer real Sets over hand-rolled Set-likes."],"tags":["javascript","set","set-like"],"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-19T12:17:13.211Z"}