Skip to content

InstanceType<T> | undefined collapses to InstanceType<T> in return type of generic method with polymorphic thisΒ #63369

@codextremist

Description

@codextremist

πŸ”Ž Search Terms

InstanceType | undefined collapses to InstanceType

πŸ•— Version & Regression Information

Tested with typescript 5.9.3

⏯ Playground Link

No response

πŸ’» Code

class Foo {
  static async test<TThis extends typeof Foo>(
    this: TThis
  ): Promise<InstanceType<TThis> | undefined> {
    return undefined
  }
}

async function test() {
const x = await Foo.test()
}

πŸ™ Actual behavior

type of x resolves to Foo, when it should resolve to Foo | undefined. The same happens with null

πŸ™‚ Expected behavior

It should have been resolved to Foo | undefined.

Additional information about the issue

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions