Skip to content

[Bug]: Type inferrence in toBeCalledWith broken for method with overloads #13337

@avaly

Description

@avaly

Version

29.1.1

Steps to reproduce

import { expect, test } from '@jest/globals';

function sampleWithOverloads(first: string, second: number): void;
function sampleWithOverloads(first: string, second: number, third: Function): void;
function sampleWithOverloads(first: string, second: number, third?: Function) {
  // no op
}

function sampleWithoutOverloads(first: string, second: number) {
  // no op
}

test('toBeCalledWith without overloads', () => {
  expect(sampleWithoutOverloads).toHaveBeenCalledWith('foo', 123);
  // ^ This is OK
});

test('toBeCalledWith with overloads', () => {
  expect(sampleWithOverloads).toHaveBeenCalledWith('foo', 123);
  // ^ This is erroring: Expected 3 arguments, but got 2. ts(2554)
});

Expected behavior

The methods with overloads should be inferred correctly based on the number of arguments.

Actual behavior

Methods with overloads are triggering type errors.

Additional context

The bug was introduced in #13268

This is a common pattern in libraries which support both callback and promise syntax, e.g. https://github.com/mongodb/node-mongodb-native/blob/main/src/collection.ts#L265-L285

Environment

System:
  OS: Linux 5.13 Ubuntu 20.04.5 LTS (Focal Fossa)
  CPU: (16) x64 AMD Ryzen 7 4800H with Radeon Graphics
Binaries:
  Node: 18.0.0 - ~/.volta/tools/image/node/18.0.0/bin/node
  Yarn: 1.22.11 - ~/.volta/tools/image/yarn/1.22.11/bin/yarn
  npm: 8.1.4 - ~/.volta/tools/image/npm/8.1.4/bin/npm
npmPackages:
  jest: 29.1.1 => 29.1.1

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions