{"record":{"id":"1b66d2abdcb0faef","repo":"karatelabs/karate","slug":"cannot-mix-bigint-and-other-types-use-explicit-conversions","errorCode":null,"errorMessage":"Cannot mix BigInt and other types, use explicit conversions (${opName})","messagePattern":"Cannot mix BigInt and other types, use explicit conversions \\((.+?)\\)","errorType":"exception","errorClass":"JsErrorException (typeError)","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/Terms.java","lineNumber":66,"sourceCode":"    public static final JsUndefined UNDEFINED = JsUndefined.INSTANCE;\n\n    static final Number NEGATIVE_ZERO = -0.0;\n\n    private Terms() {\n        // static holder - the binary operators take (lhs, rhs) directly\n    }\n\n    // True iff either operand is BigInt. Fast path: most call sites have\n    // plain Number operands and this returns false on the first instanceof.\n    private static boolean isBigIntOp(Number lhs, Number rhs) {\n        return lhs instanceof BigInteger || rhs instanceof BigInteger;\n    }\n\n    // Spec: arithmetic ops require both operands to be BigInt or both Number;\n    // mixing throws TypeError. Centralized check fires only on the rare path.\n    private static void requireBothBigInt(Number lhs, Number rhs, String opName) {\n        if (!(lhs instanceof BigInteger) || !(rhs instanceof BigInteger)) {\n            throw JsErrorException.typeError(\n                \"Cannot mix BigInt and other types, use explicit conversions (\" + opName + \")\");\n        }\n    }\n\n    static Number parseInt(String str, int radix) {\n        if (str == null) {\n            return Double.NaN;\n        }\n        str = str.trim();\n        if (str.isEmpty()) {\n            return Double.NaN;\n        }\n        boolean negative = false;\n        int index = 0;\n        if (str.charAt(0) == '-') {\n            negative = true;\n            index++;\n        } else if (str.charAt(0) == '+') {","sourceCodeStart":48,"sourceCodeEnd":84,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/Terms.java#L48-L84","documentation":"A TypeError implementing the ECMAScript BigInt mixing rule: arithmetic/bitwise operations (*, &, |, ^, >>, <<) require both operands to be BigInt or both to be Number. Karate's requireBothBigInt check throws when one operand is a BigInteger and the other is not, exactly as the JS spec mandates.","triggerScenarios":"Evaluating `bigIntValue * 2`, `bigIntValue & otherNonBigInt`, `bigintValue << 1` etc. in the embedded JS engine where one side came from a Java BigInteger (bridged value or `BigInt()`/`123n`) and the other is a plain JS number.","commonSituations":"Doing math on Java-bridged BigInteger values with numeric literals, JSON-parsed numbers (always JS numbers) combined with BigInt values, incremental migration of expressions to BigInt.","solutions":["Convert the plain number to BigInt: `BigInt(2)` or use the `n` literal form if supported, so both operands are BigInt.","Convert the BigInt down to a Number when the value is small enough: `Number(bigIntValue)` before mixing.","Use explicit conversion helpers consistently at the boundary where Java BigInteger values enter the script.","Normalize both operands at the start of the expression rather than mid-expression."],"exampleFix":"// before\n* eval bigIntValue * 2\n// after\n* eval bigIntValue * BigInt(2)","handlingStrategy":"validation","validationCode":"// ensure both operands are BigInt before arithmetic\nif (typeof a !== 'bigint') a = BigInt(a);","typeGuard":"function bothBigInt(a, b) { return typeof a === 'bigint' && typeof b === 'bigint'; }","tryCatchPattern":null,"preventionTips":["Convert Java BigInteger inputs to BigInt at the boundary, once.","Use BigInt() literals/constructors on every numeric constant in BigInt expressions.","Downconvert to Number early when values are small.","Write helper functions that normalize operand types before math."],"tags":["javascript","type-error","bigint","arithmetic"],"backgroundTag":"type-mismatch","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"}