Io-ts: Bug?:Crash when used simultaneously with taggedUnion and recursion

Created on 12 Apr 2019  路  2Comments  路  Source: gcanti/io-ts

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]);

Most helpful comment

@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]))

All 2 comments

@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

Was this page helpful?
0 / 5 - 0 ratings