arktype icon indicating copy to clipboard operation
arktype copied to clipboard

Collapse common parts of union

Open ssalbdivad opened this issue 1 year ago • 0 comments

You can reduce a union of two structures, neither of which is a subtype of the other, iff there exists some key K such that K is present in both structures and such that the set of entries other than K is identical for both structures, both in terms of keys and values.

This would allow the following types to reduce to equivalent:

const t = type({ a: "string" }, "|", { a: "number" });

const u = type({
  a: "string|number",
});

Also if you think about how 1<number<10|10<=number<100 works, it is the only case I can think of where despite the two branches being disjoint, they can be reduced when unioned. Perhaps this could be handled with the same underlying logic?

ssalbdivad avatar May 09 '23 14:05 ssalbdivad