jestjs/jest · error · TypeError

${pkg}: ${name} typeof ${type} is not a function

Error message

${pkg}: ${name} typeof ${type} is not a function

What it means

diff-sequences requires two callbacks: isCommon(indexA, indexB) and foundSubsequence(nCommon, aCommon, bCommon). validateCallback throws a TypeError if either is not a function, because the algorithm calls them during traversal and a non-function would otherwise crash deeper in the stack with a less helpful message.

Source

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

  }
};

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,
// return by output function the number of adjacent items and starting indexes
// of each common subsequence.
export default function diffSequence(
  aLength: number,
  bLength: number,
  isCommon: IsCommon,
  foundSubsequence: FoundSubsequence,
): void {
  validateLength('aLength', aLength);
  validateLength('bLength', bLength);
  validateCallback('isCommon', isCommon);
  validateCallback('foundSubsequence', foundSubsequence);

View on GitHub (pinned to 8e6d128e4a)

Solutions

  1. Supply both required function arguments: `diffSequence(aLen, bLen, isCommon, foundSubsequence)` where the last two are functions.
  2. Verify the function references are defined at the call site (watch for typos and lost `this`).
  3. If a callback is optional in your wrapper, pass a no-op: `() => true` for isCommon or `() => {}` for foundSubsequence.

Example fix

// before
diffSequence(a.length, b.length, isCommon); // missing foundSubsequence -> undefined

// after
diffSequence(a.length, b.length, isCommon, (n, aIdx, bIdx) => {
  console.log('common run of', n, 'at', aIdx, bIdx);
});
Defensive patterns

Strategy: type-guard

Validate before calling

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

function runDiff(aLen, bLen, isCommon, found) {
  if (typeof isCommon !== 'function') throw new TypeError('isCommon must be a function');
  if (typeof found !== 'function') throw new TypeError('foundSubsequence must be a function');
  return diffSequence(aLen, bLen, isCommon, found);
}

Type guard

function isFunction(v: unknown): v is (...args: any[]) => any {
  return typeof v === 'function';
}

if (!isFunction(isCommon) || !isFunction(foundSubsequence)) {
  throw new TypeError('callbacks must be functions');
}

Prevention

When it happens

Trigger: Calling diffSequence with a non-function for isCommon or foundSubsequence — e.g. passing an object, undefined, or forgetting the argument. Also when a callback is destructured/renamed incorrectly and ends up undefined.

Common situations: Copying a diff-sequences example and dropping one of the two callbacks; passing a method reference that lost its `this` binding and is actually undefined after destructuring; integrating from loosely-typed JS.

Related errors


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