Skip to content

Cannot specify the type of an expression involving a Class returned from a functionΒ #43923

Closed

Description

@ssledorze

Bug Report

πŸ”Ž Search Terms

path dependent, untypable, nested class type, nested class reference

πŸ•— Version & Regression Information

No version known which works correctly

⏯ Playground Link

https://www.typescriptlang.org/play?jsx=0&ts=4.2.3#code/MYewdgzgLgBAjjAvDAPAFQHwAoAeAuGNASiQwCgYZgAbAQwghgGEYBvCymAWyRhw8qhIUAE4BXYFBAisJVgF8OiskOjxavMAFMA7jCxwsAIgBMRokVkwA9NZhQAngActMAJYQCcFNBFuwAOYYAHRMKuBqcABGXj6i-kGhmrr6hqbmliS2MACiIiLSZGTZAHIgUK5QABa0sNUe7oy01BAgMLRgjRBirjpu1TBg4DBOINQOXNJOVW7AVCAAJlpFqrAA1rxWiOSCdAzMbALzwuKS0lYKSisR6xrI2npYa7KZNnaOLo0EYaswazEHe4pJ4vKzZPIFERFIA

πŸ’» Code

const q = <T>(x: T) =>
  class C {
    m = x
    constructor() {}
  }

const qa = new (q("2"))() // type is: q<string>.C
const qb: q<string>.C = new (q("2"))() // Error

// Note that this is also an issue with non polymorphic code

const k = () =>
  class C {
    constructor() {}
  }

const ka = new (k())() // type is: C
const kb: C = new (k())() // Error

πŸ™ Actual behavior

I can't explicitly type the expressions

πŸ™‚ Expected behavior

I would expect to be able to type this expression.
Ideally using the signature inferred by the compiler

Activity

  1. naorzr commented on May 3, 2021

    @naorzr

    It's possible to infer the type by doing

    const ka = new (k())() // type is: C
    const kb: InstanceType<typeof ka> = new (k())() 
    
  2. whzx5byb commented on May 3, 2021

    @whzx5byb

    For non-polymorphic version:

    const kb: InstanceType<ReturnType<typeof k>> = new (k())();
    

    Unfortunately it doesn't work in the polymorphic version, because ts doesn't support Generic Values.
    There are some workarounds in https://stackoverflow.com/a/59072991/3016883 and https://stackoverflow.com/a/62724646/3016883

  3. RyanCavanaugh commented on May 3, 2021

    @RyanCavanaugh
    Member

    const doesn't create a new type; this is the intended behavior. There are many ways to refer to this type depending on what you want to do with it.

  4. ssledorze commented on May 3, 2021

    @ssledorze
    Author

    Ryan Cavanaugh (@RyanCavanaugh) thanks for having taken the time to review this issue;
    are all the ways to refer to this type enumerated above or do you know others? (or know where I can find others)
    Thanks again!

  5. typescript-bot commented on May 6, 2021

    @typescript-bot
    Contributor

    This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

  6. locked as resolved and limited conversation to collaborators on Oct 21, 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 IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions