microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
100.7k stars 12.45k forks source link

Instantiation expression inside nested classes produces unexpected circular reference error #52892

Open whzx5byb opened 1 year ago

whzx5byb commented 1 year ago

Bug Report

πŸ”Ž Search Terms

instantiation expression nested class

πŸ•— Version & Regression Information

⏯ 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
jakebailey commented 1 year ago

I suspect #52813 is related. We'll see.