在Typescript中递归泛型?

Typescript 使用以下代码引发错误:

type AndType<AstT> = { type: 'And', args: [AstT] };
type TrueType = { type: 'True' };
type BaseAST = AndType<BaseAST> | TrueType;

抱怨Type alias 'BaseAST' circularly references itself.;但是,如果我将循环引用包装在一个对象中,则这些类型可以正常编译:

type AndType<AstT> = { type: 'And', args: [AstT] };
type TrueType = { type: 'True' };
type BaseAST = {value: AndType<BaseAST>} | {value: TrueType};

为什么?有没有人参考定义此行为的文档?

以上是在Typescript中递归泛型?的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>