Generate a typescript interface that references itself

可紊 提交于 2020-03-05 04:08:06

问题


EDIT: I have rewritten this question to be much clearer.

I am trying to programmatically infer an interface which references itself.

I am writing some code which can encode a recursive structure in binary. For example:

const myTreeEncoder = Struct(Self => ({
    name: Str,
    children: List(Self)
});

This can encode structures like this from binary and back:

const someUint8Array = myTreeEncoder.encode({
    name: 'parent',
    children: [{
        name: 'child',
        children: []
    }]
});

The problem is, how can the encode function be typed correctly so it expects an input of:

interface BinaryTree {
    name: string;
    children: BinaryTree[];
}

Here is a minimal reproducible example

来源:https://stackoverflow.com/questions/60305599/generate-a-typescript-interface-that-references-itself

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!