Skip to content

Inference fails for generic-typed constructor parameter when no explicit constructor is present #47340

@nex3

Description

@nex3

Bug Report

I'm trying to write a function that takes a (generic) constructor and calls it to instantiate a class. The type inference for such a function normally works fine, but if the constructor is for a generic class it fails even if the generic parameter has a default.

🔎 Search Terms

generic constructor parameter inference instanceof

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about constructor parameters

⏯ Playground Link

Playground link with relevant code

💻 Code

class Foo<K extends {} = {}> {}

function create<T>(type: new () => T): T {
    return new type();
}

let string = create(String); // has type String

let foo = create(Foo); // should have type Foo<{}>, has type unknown

🙁 Actual behavior

The foo variable has type unknown.

🙂 Expected behavior

I expected the foo variable to have type Foo<{}>.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions