Skip to content

Unexpected overload signature is matched with constructor typeΒ #51899

Closed

Description

Bug Report

πŸ”Ž Search Terms

overload, constructor type

πŸ•— Version & Regression Information

I tested with different TS v4 versions. This bug does not seem like a regression.

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

type Type<T> = new (...args: any[]) => T;

function inject<T>(t: Type<T>): T;
function inject(t: any): any; // comment out this line => the type of `c2` will be `ClassWithCtorArgs`
function inject(t: any): any {
    return t;
}

class Class {}

class ClassWithCtorArgs {
    constructor(x: number) {}
}

// type: Class βœ…
const c1 = inject(Class);

// type: any ❌
const c2 = inject(ClassWithCtorArgs);

πŸ™ Actual behavior

The type of c2 constant from the example is any.

πŸ™‚ Expected behavior

The type of c2 constant from the example should be ClassWithCtorArgs.

Activity

  1. added
    Working as Intended
    on Dec 15, 2022
  2. RyanCavanaugh commented on Dec 15, 2022

    @RyanCavanaugh
    Member

    The overload resolution algorithm is deliberately very simple. It proceeds as follows:

    • Pick the first signature which matches according to the subtype relation
    • If none was chosen, pick the first signature which matches according to the assignability relation

    One of the differences between subtype and assignable is how any gets treated in function parameters, as shown here, so the second overload gets picked during the first pass.

    I recommend using a different definition of Type<T>:

    type Type<T> = new (...args: never[]) => T;
  3. markostanimirovic commented on Dec 15, 2022

    @markostanimirovic
    Author

    Thank you so much Ryan Cavanaugh (@RyanCavanaugh)! :)

  4. locked as resolved and limited conversation to collaborators on Oct 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as Intended

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions