effect icon indicating copy to clipboard operation
effect copied to clipboard

S.taggedUnion for easier creation of tagged unions vs S.union and S.attachPropertySignature

Open jessekelly881 opened this issue 2 years ago • 0 comments

Instead of having to use S.union with S.attachPropertySignature directly to create tagged objects, a simpler solution might be to add a S.attachSignatures helper that fn that abstracts this behavior. The naming might not be the best but the idea is that the original api and the alternative api below produce identical schemas.

const Circle = S.struct({ radius: S.number });
const Square = S.struct({ sideLength: S.number });

// original api

const DiscriminatedShape = S.union(
  Circle.pipe(S.attachPropertySignature("kind", "circle")),
  Square.pipe(S.attachPropertySignature("kind", "square")),
); 

// alternative (/complimentary) api

S.attachSignatures("kind", {
  "circle": Circle,
  "square": Square
})

jessekelly881 avatar Aug 06 '23 02:08 jessekelly881