microsoft / microsoft/TypeScript

Static method on abstract class with generic this parameter causes "excessively deep" error when called on concrete class

Open
#60,992 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Domain: check: Type Circularity Help Wanted Possible Improvement
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

🔎 Search Terms
  • abstract class
  • static method
  • this parameter
  • excessively deep
  • concrete class
  • generic
🕗 Version & Regression Information

This is broken in 5.6, 5.7, and nightly at least.

⏯ Playground Link

https://www.typescriptlang.org/play/?target=99&ts=5.8.0-dev.20250117&ssl=6&ssc=12&pln=6&pc=15#code/IYIwzgLgTsDGEAJYBthjAgguacIGFV0AeAEQD4EBvAKAXoQHMBTRAE2AmAAoBKALgSlqdBmKisArlAB21AL4I0CSTIDWMgPYB3OctKj68moYSROAS1gIAHgE8AXsQAqCZjYjMZbDBDsAHZk0AMywcGHhCNDBiYBk7cnJuUzEIAAsLMEFnABoUhg4ubLcPLx8EP0CQsMgIgiIYixlg5ighSgB+IQRBGWYAN1bTXgUaYxoUaIR8TRlYCU8o9BLPbwxsWrwlmNqmxkpaMZMZuYXmbYA6ewduAEYAJgBmXiA

💻 Code
abstract class AbstractClass<D> {
    get data(): D {
        return {} as unknown as D
    }

    static xyz<T extends typeof AbstractClass<any>>(
        this: T,
        data: T extends typeof AbstractClass<infer D> ? D : never
    ) {}
}

class ConcreteClass extends AbstractClass<string> {

}

ConcreteClass.xyz(123)
🙁 Actual behavior

The ConcreteClass.xyz results in Type instantiation is excessively deep and possibly infinite. and as a result infers D as never.

🙂 Expected behavior

This to work fine. A similar case, but where InstanceType<T> extends AbstractClass<infer D> ? D : never works fine: https://www.typescriptlang.org/play/?target=99&ts=5.8.0-dev.20250117#code/IYIwzgLgTsDGEAJYBthjAgguacIGFV0AeAEQD4EBvAKAXoQHMBTRAE2AmAAoBKALgSlqdBmKisArlAB21AL4I0CSTIDWMgPYB3OctKj68moYSROAS1gIAHgE8AXsQAqCZjYjMZbDBDsAHZk0AMywcGHhCNDBiYBk7cnJuUzEIAAsLMEFnABoUhg4uQQBJGXMZWGZnAOYXSndPbwxsSAiCIhiLGWDmKCFKAH4hBEEZZgA3XtNeBRpjGhRohHxNColPKPQ3Dy8fMNa8TZjWrsZKWjmTFbXWZiOAOnsHbgBGACYAZl4gA

Additional information about the issue

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided TypeScript Playground reproduction and compare it with the working InstanceType variant. Trace how the compiler handles the static this parameter and conditional type inference; done means ConcreteClass.xyz no longer reports an excessively deep instantiation error and infers the intended type.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.