Skip to content

Incorrect type inference for generic types with 'prototype' property #55199

Closed
@ApplY3D

Description

@ApplY3D

Bug Report

🔎 Search Terms

  • generic type prototype inference

🕗 Version & Regression Information

  • This changed between versions 4.7.4 and 5.2.0
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about prototype

⏯ Playground Link

Playground link with relevant code

💻 Code

interface Type<T> {
    new(...args: any[]): T;
}
interface Proto<T> {
    prototype: T;
}

function injectWithType<T>(a: Type<T>): T { return null as T };
{
    const okUnknown = injectWithType(Set); // Set<unknown> ⚠️
    const okNumber = injectWithType<Set<number>>(Set); // Set<number> ✅
    const okNumber2 = injectWithType(Set<number>); // Set<number> ✅
}

function injectWithProto<T>(a: Proto<T>): T { return null as T };
{
    const okAny = injectWithProto(Set); // Set<any> ⚠️
    const okNumber = injectWithProto<Set<number>>(Set); // Set<number> ✅
    const unexpectedAny = injectWithProto(Set<number>); // Set<any> ‼️
}

The compiled version has a bug too - double parentheses (reproduced both with the injectWithType and the injectWithProto.):

const unexpectedAny = injectWithProto((Set)) // >= typescript@4.7.4
const unexpectedAny = injectWithProto(Set()) // < typescript@4.7.4

🙁 Actual behavior

When using an interface or atype with the prototype property to declare class, generic type is getting wrong, especially in the ExpressionWithTypeArguments (‼️) case.

🙂 Expected behavior

(⚠️) - These lines should have the same type (Set<any> or Set<unknown>)
(‼️) - This line should correctly infer generic type (Set<number>)

const arrOfNum = injectWithProto(Set<number>) // should be Set<number>, not Set<any>

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs More InfoThe issue still hasn't been fully clarified

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions