jestjs/jest · error · TypeError

: typeof is not a number

Error message

${pkg}: ${name} typeof ${typeof arg} is not a number

What it means

@jest/diff-sequences validates its inputs before computing a longest common subsequence. validateLength throws a TypeError when aLength or bLength is not of type 'number' (e.g. undefined, a string, an object). The check runs for both length arguments before any diffing work, so the function fails fast on misuse rather than producing a nonsensical result.

Solutions

  1. Pass the numeric length of each sequence, not the sequence itself: `diffSequence(a.length, b.length, ...)`.
  2. Ensure the length value is actually defined at the call site (guard optional inputs with a default of 0).
  3. If you have unknown inputs, coerce/validate with Number.isInteger before calling.

Example fix

// before
diffSequence(arrA, arrB, isCommon, foundSubsequence); // passed arrays, not lengths

// after
diffSequence(arrA.length, arrB.length, isCommon, foundSubsequence);
Defensive patterns

Strategy: type-guard

Validate before calling

import diffSequence from '@jest/diff-sequences';

function safeDiff(aLen, bLen, isCommon, found) {
  if (typeof aLen !== 'number' || typeof bLen !== 'number') {
    throw new TypeError('lengths must be numbers');
  }
  return diffSequence(aLen, bLen, isCommon, found);
}

Type guard

function isNumber(v: unknown): v is number {
  return typeof v === 'number';
}

// usage:
if (!isNumber(aLen) || !isNumber(bLen)) {
  throw new TypeError('aLength and bLength must be numbers');
}
diffSequence(aLen, bLen, isCommon, found);

Prevention

When it happens

Trigger: Calling diffSequence(aLength, bLength, isCommon, foundSubsequence) where one of the length arguments is undefined, null, a string, or any non-number — for example passing the array itself instead of its `.length`, or forgetting an argument.

Common situations: Passing arrays/objects where a numeric length is expected; computing a length from an optional field that is undefined; integrating diff-sequences with untyped JS that supplies the wrong shape; off-by-one in argument order when copying an example.

Related errors


AI-assisted analysis of jestjs/jest@8e6d128e4a (2026-08-10). Data as JSON: /api/errors/0cdef8d190fc9c97. Report an issue: GitHub.

Appendix: source

Thrown at packages/diff-sequences/src/index.ts:761

    // Recursely find and return common subsequences following the division.
    findSubsequences(
      nChangeFollowing,
      aStartFollowing,
      aEnd,
      bStartFollowing,
      bEnd,
      transposed,
      callbacks,
      aIndexesF,
      aIndexesR,
      division,
    );
  }
};

const validateLength = (name: string, arg: unknown) => {
  if (typeof arg !== 'number') {
    throw new TypeError(`${pkg}: ${name} typeof ${typeof arg} is not a number`);
  }
  if (!Number.isSafeInteger(arg)) {
    throw new RangeError(`${pkg}: ${name} value ${arg} is not a safe integer`);
  }
  if (arg < 0) {
    throw new RangeError(`${pkg}: ${name} value ${arg} is a negative integer`);
  }
};

const validateCallback = (name: string, arg: unknown) => {
  const type = typeof arg;
  if (type !== 'function') {
    throw new TypeError(`${pkg}: ${name} typeof ${type} is not a function`);
  }
};

// Compare items in two sequences to find a longest common subsequence.
// Given lengths of sequences and input function to compare items at indexes,

View on GitHub (pinned to 8e6d128e4a)