TypeScript 嵌套类内的示例化表达式产生意外的循环引用错误,

tv6aics1  于 2个月前  发布在  TypeScript
关注(0)|答案(1)|浏览(40)

Bug报告

🔎 搜索词

示例化表达式嵌套类

🕗 版本与回归信息

  • 在我尝试的每个版本中都有这种行为

⏯ Playground链接

带有相关代码的Playground链接

💻 代码

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
//              ^?

🙁 实际行为

显示错误 'child' is referenced directly or indirectly in its own type annotation.
然而,取消注解 // static {...} 时,它只能在Playground中工作。在VSCode中,它仍然显示上面的错误。

🙂 预期行为

a.child.parent.value 应为 number

解决方法

解决方法是移除 class B<T> 的默认类型。以下代码可以正常工作,但仅限于Playground。遗憾的是,在我的开发版本5.0.0-dev.20230216的VSCode中,它仍然会出错。

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

相关问题