karatelabs/karate · error · JsErrorException (typeError)

Cannot mix BigInt and other types, use explicit conversions

Error message

Cannot mix BigInt and other types, use explicit conversions (${opName})

What it means

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.

Solutions

  1. Convert the plain number to BigInt: `BigInt(2)` or use the `n` literal form if supported, so both operands are BigInt.
  2. Convert the BigInt down to a Number when the value is small enough: `Number(bigIntValue)` before mixing.
  3. Use explicit conversion helpers consistently at the boundary where Java BigInteger values enter the script.
  4. Normalize both operands at the start of the expression rather than mid-expression.

Example fix

// before
* eval bigIntValue * 2
// after
* eval bigIntValue * BigInt(2)
Defensive patterns

Strategy: validation

Validate before calling

// ensure both operands are BigInt before arithmetic
if (typeof a !== 'bigint') a = BigInt(a);

Type guard

function bothBigInt(a, b) { return typeof a === 'bigint' && typeof b === 'bigint'; }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12). Data as JSON: /api/errors/1b66d2abdcb0faef. Report an issue: GitHub.

Appendix: source

Thrown at karate-js/src/main/java/io/karatelabs/js/Terms.java:66

    public static final JsUndefined UNDEFINED = JsUndefined.INSTANCE;

    static final Number NEGATIVE_ZERO = -0.0;

    private Terms() {
        // static holder - the binary operators take (lhs, rhs) directly
    }

    // True iff either operand is BigInt. Fast path: most call sites have
    // plain Number operands and this returns false on the first instanceof.
    private static boolean isBigIntOp(Number lhs, Number rhs) {
        return lhs instanceof BigInteger || rhs instanceof BigInteger;
    }

    // Spec: arithmetic ops require both operands to be BigInt or both Number;
    // mixing throws TypeError. Centralized check fires only on the rare path.
    private static void requireBothBigInt(Number lhs, Number rhs, String opName) {
        if (!(lhs instanceof BigInteger) || !(rhs instanceof BigInteger)) {
            throw JsErrorException.typeError(
                "Cannot mix BigInt and other types, use explicit conversions (" + opName + ")");
        }
    }

    static Number parseInt(String str, int radix) {
        if (str == null) {
            return Double.NaN;
        }
        str = str.trim();
        if (str.isEmpty()) {
            return Double.NaN;
        }
        boolean negative = false;
        int index = 0;
        if (str.charAt(0) == '-') {
            negative = true;
            index++;
        } else if (str.charAt(0) == '+') {

View on GitHub (pinned to a22eb90246)