io-ts:1.8.5
import * as t from "io-ts";
const A: t.Type<{ type: "a", children: t.TypeOf<typeof Node>[] }> = t.recursion("A", () =>
t.strict({
type: t.literal("a"),
children: t.array(Node)
})
);
const B: t.Type<{ type: "b", children: t.TypeOf<typeof Node>[] }> = t.recursion("B", () =>
t.strict({
type: t.literal("b"),
children: t.array(Node)
})
);
const Node = t.taggedUnion("type", [A, B]);
@kgtkr Node is recursive too
interface A {
type: 'a'
children: Array<A | B>
}
const A: t.Type<A> = t.recursion('A', () =>
t.strict({
type: t.literal('a'),
children: t.array(Node)
})
)
interface B {
type: 'b'
children: Array<A | B>
}
const B: t.Type<B> = t.recursion('B', () =>
t.strict({
type: t.literal('b'),
children: t.array(Node)
})
)
const Node: t.Type<A | B> = t.recursion('Node', () => t.taggedUnion('type', [A, B]))
@gcanti Thank you
Most helpful comment
@kgtkr
Nodeis recursive too