jestjs/jest · error · TypeError
@jest/diff-sequences: ${name} typeof ${typeof arg} is not a
Error message
@jest/diff-sequences: ${name} typeof ${typeof arg} is not a number What it means
diffSequence(aLength, bLength, isCommon, foundSubsequence) calls validateLength on each length. The first guard requires `typeof arg === 'number'`; anything else (undefined, string, null, object) throws TypeError. This is a hard precondition because the algorithm indexes into sequences 0..length and cannot proceed on a non-numeric length.
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 f49721c78e)
Solutions
- Pass the actual numeric lengths, typically `a.length` and `b.length` of the two arrays/strings.
- Add an explicit coercion or guard at the call site: `Number.isFinite(x) ? x : 0`.
- Re-order arguments to match the signature (aLength, bLength, isCommon, foundSubsequence).
Example fix
// before diffSequence(arr.length, other.length, 'isCommon', cb); // 3rd arg wrong, but length-like; real bug: diffSequence(undefined, other.length, isCommon, foundSubsequence); // after diffSequence(arr.length, other.length, isCommon, foundSubsequence);
Defensive patterns
Strategy: type-guard
Validate before calling
if (typeof aLength !== 'number' || typeof bLength !== 'number') {
throw new TypeError('diffSequence lengths must be numbers');
}
diffSequence(aLength, bLength, isCommon, foundSubsequence); Type guard
function isLength(x: unknown): x is number {
return typeof x === 'number' && Number.isSafeInteger(x) && x >= 0;
} Try / catch
try {
diffSequence(aLength, bLength, isCommon, foundSubsequence);
} catch (e) {
if (e instanceof TypeError && /is not a number/.test(e.message)) { /* coerce or reject */ }
throw e;
} Prevention
- Always derive lengths from real collections (Array.length, String.length).
- Type the wrapper's parameters so the compiler rejects non-numbers at the call site.
- Forward lengths through explicit numeric variables, not `any`.
When it happens
Trigger: Calling diffSequence with aLength or bLength that is not a number: passing `undefined` (forgot an argument), a string like '"5"', null, or an object. Direct internal calls in Jest's diff/output code that mistakenly forward an uncomputed length also land here.
Common situations: Wrapping @jest/diff-sequences to build a custom differ and forgetting to coerce Array.length; refactoring that swaps argument order; passing a getter result that returned undefined.
Related errors
- @jest/diff-sequences: ${name} value ${arg} is not a safe int
- @jest/diff-sequences: ${name} value ${arg} is a negative int
- @jest/diff-sequences: ${name} typeof ${type} is not a functi
- any() expects to be passed a constructor function. Please pa
- expect.customEqualityTesters: Must be set to an array of Tes
AI-assisted analysis of jestjs/jest@f49721c78e (2026-08-03).
Data as JSON: /data/errors/6fa83d0344975293.json.
Report an issue: GitHub.