Skip to content

Inference of generic constructor function return type. #32976

@MicahZoltu

Description

@MicahZoltu

TypeScript Version: 3.7.0-dev.20190817

Search Terms:
constructor type inference generic

Code

declare class Banana<T extends string> { constructor(a: boolean, property: T) }

declare function fruitFactory1<TFruit>(Fruit: new (...args: any[]) => TFruit): TFruit
declare function fruitFactory2<TFruit>(Fruit: new (a: boolean, ...args: any[]) => TFruit): TFruit

const banana1 = fruitFactory1(Banana) // Banana<string>
const banana2 = fruitFactory2(Banana) // INFERENCE FAILURE! Banana<any>

Expected behavior:
banana2 would be of type Banana<string>

Actual behavior:
banana2 is of type Banana<any>

Playground Link: Playground

Related Issues: Not that I could tell, but I don't fully grok the problem.

Notes from Gitter:

it's because we don't view Banana's T as an inference target for a call to fruitFactory -- only fruitFactory's type parameters. So we just Choose Something Good.

[the working case, banana1] is just defaulting it from the constraint for some reason. I'm not sure why without doing lots of poking through the code.

I'm pretty sure fruitFactory2 only works by 'mistake', in that we use the constraint string as the default value for T

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions