{"id":"6fa83d0344975293","repo":"jestjs/jest","slug":"jest-diff-sequences-name-typeof-typeof-arg","errorCode":null,"errorMessage":"@jest/diff-sequences: ${name} typeof ${typeof arg} is not a number","messagePattern":"@jest/diff-sequences: (.+?) typeof (.+?) is not a number","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/diff-sequences/src/index.ts","lineNumber":761,"sourceCode":"    // Recursely find and return common subsequences following the division.\n    findSubsequences(\n      nChangeFollowing,\n      aStartFollowing,\n      aEnd,\n      bStartFollowing,\n      bEnd,\n      transposed,\n      callbacks,\n      aIndexesF,\n      aIndexesR,\n      division,\n    );\n  }\n};\n\nconst validateLength = (name: string, arg: unknown) => {\n  if (typeof arg !== 'number') {\n    throw new TypeError(`${pkg}: ${name} typeof ${typeof arg} is not a number`);\n  }\n  if (!Number.isSafeInteger(arg)) {\n    throw new RangeError(`${pkg}: ${name} value ${arg} is not a safe integer`);\n  }\n  if (arg < 0) {\n    throw new RangeError(`${pkg}: ${name} value ${arg} is a negative integer`);\n  }\n};\n\nconst validateCallback = (name: string, arg: unknown) => {\n  const type = typeof arg;\n  if (type !== 'function') {\n    throw new TypeError(`${pkg}: ${name} typeof ${type} is not a function`);\n  }\n};\n\n// Compare items in two sequences to find a longest common subsequence.\n// Given lengths of sequences and input function to compare items at indexes,","sourceCodeStart":743,"sourceCodeEnd":779,"githubUrl":"https://github.com/jestjs/jest/blob/f49721c78e195558b40913977c9230f5b7f559d8/packages/diff-sequences/src/index.ts#L743-L779","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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)."],"exampleFix":"// before\ndiffSequence(arr.length, other.length, 'isCommon', cb); // 3rd arg wrong, but length-like; real bug:\ndiffSequence(undefined, other.length, isCommon, foundSubsequence);\n\n// after\ndiffSequence(arr.length, other.length, isCommon, foundSubsequence);","handlingStrategy":"type-guard","validationCode":"if (typeof aLength !== 'number' || typeof bLength !== 'number') {\n  throw new TypeError('diffSequence lengths must be numbers');\n}\ndiffSequence(aLength, bLength, isCommon, foundSubsequence);","typeGuard":"function isLength(x: unknown): x is number {\n  return typeof x === 'number' && Number.isSafeInteger(x) && x >= 0;\n}","tryCatchPattern":"try {\n  diffSequence(aLength, bLength, isCommon, foundSubsequence);\n} catch (e) {\n  if (e instanceof TypeError && /is not a number/.test(e.message)) { /* coerce or reject */ }\n  throw e;\n}","preventionTips":["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`."],"tags":["diff-sequences","validation","type-error","precondition"],"analyzedSha":"f49721c78e195558b40913977c9230f5b7f559d8","analyzedAt":"2026-08-03T20:16:28.571Z","schemaVersion":2}