microsoft / microsoft/TypeScript

Instantiation expression inside nested classes produces unexpected circular reference error

Open
#52,892 1 comment 1 reaction 1 assignee View on GitHub

@jakebailey is already working on this.

Since Feb 22, 2023.

Needs Investigation Rescheduled
Dominant language
Go
Stars
111k
Forks
14.4k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

Bug Report

🔎 Search Terms

instantiation expression nested class

🕗 Version & Regression Information
  • This is the behavior in every version I tried
⏯ Playground Link

Playground link with relevant code

💻 Code
class A<T = number> {
    //static { type _<T extends A = A> = 0 }
    //^ will work when uncomment it?

    value!: T;
    child!: InstanceType<typeof A.B<A<T>>>

    static B = class B<T extends A = A> {
        parent!: T;
    } 
}

var a = new A
a.child.parent.value
//              ^?
🙁 Actual behavior

Shows an error 'child' is referenced directly or indirectly in its own type annotation..

However, when uncommenting // static {...} it will work only in playground.
In VSCode it still shows the error above.

🙂 Expected behavior

a.child.parent.value is number

Workaround

A workaround is remove the default type for class B<T>. The code below works well, also, only in playground. In my VSCode(5.0.0-dev.20230216) it still breaks unfortunately.

class A<T extends number = number> {
    value!: T;
    child!: InstanceType<typeof A.B<A<T>>>

    static B = class B<T extends A> {
        parent!: T;
    }  
}

var a = new A
a.child.parent.value
//                ^? number

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.