Skip to content

@types/jasmine toHaveBeenCalledWith infers parameters for overloads incorrectly #42455

@devversion

Description

@devversion

The function signature for toHaveBeenCalledWith unfortunately breaks if a generic function that consists of overloads is passed. TypeScript is not able to infer the proper overload for functions through generics. Only a single overload signature is considered for parameter inferring. For example:

function hello(idx: number): void;
function hello(item: object): void;
function hello(arg: any): void { /* impl */}

// This won't work because `MatchableArgs` only infers `object` as parameter type.
expect(hello).toHaveBeenCalledWith(1);

This is a known limitation in TypeScript, but it feels inconvenient that workarounds are needed for function overloads. Overloads are not a rare concept seen in TypeScript projects. I'm wondering if it would make sense to go back to the non-inferred parameter types. i.e. simply using any[].

Authors: @zvirja @djungowski @kolodny

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions