Handling Union Types with Diverse Structures in TypeScript
How do you folks deal with edges that produce a bunch of arbitrarily unioned data? For example, one shape for when X happened, one shape for when Y happened, and one shape for when an error occurred. In code:
This produces a type like:
And when working with a type like that, discriminating the union requires exhaustively checking all fields, which
To expand this way of working to n cases, I wrote a little
Now my type looks like this:
I think this is neat, and it allows me to avoid doing exhaustive property checking. But now I need to manually create type guards, a pattern matching helper, and whatever other utils a type like Either provides out of the box
it's a lot of boilerplate.
Is this something you have run into, and are there solutions out there that let me create my own union types like, deriving type guards and pattern matching?
This produces a type like:
And when working with a type like that, discriminating the union requires exhaustively checking all fields, which
S.is could do for me. However, I really like working with S.eitherFromUnion for scenarios where there's only two cases. This causes Schema to tag my types for me upon decoding.To expand this way of working to n cases, I wrote a little
tagged utility that uses S.transformOrFail to tag my type with a given tag:Now my type looks like this:
I think this is neat, and it allows me to avoid doing exhaustive property checking. But now I need to manually create type guards, a pattern matching helper, and whatever other utils a type like Either provides out of the box
Is this something you have run into, and are there solutions out there that let me create my own union types like, deriving type guards and pattern matching?
