typescript 带参数的命名空间中的循环引用

aiazj4mn  于 2023-04-13  发布在  TypeScript
关注(0)|答案(1)|浏览(141)

我正在生成包含错误的TypeScript代码。为了生成正确的代码,我需要了解以下示例中发生了什么:
下面的代码片段导致“循环引用”错误的原因是什么:

type Dict<T> = { [key:string]: T }

export namespace X {

    export type A<T> =
        | ['em', X.B<T>] //<-- error on this line

    export type B<T> = Dict<X.A<T>> //<-- and error on this line
}

但是如果我删除对命名空间的引用,就没有问题了:

type Dict<T> = { [key:string]: T }

export namespace Y {

    export type A<T> =
        | ['em', B<T>] //<----

    export type B<T> = Dict<X.A<T>>
}

如果我删除type参数,也没有问题:

type Dict<T> = { [key:string]: T }

export namespace Z {

    export type A =
        | ['em', Z.B]

    export type B = Dict<Z.A>
}
bkkx9g8r

bkkx9g8r1#

这个链接:https://github.com/microsoft/TypeScript/issues/41164让我意识到,在我的例子中,我可以用“Dict”接口代替“Dict”类型。在这些“循环引用”的情况下,接口更健壮。

相关问题