Skip to content

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

Open
@jessekelly881

Description

@jessekelly881

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
})

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs ProposalThis issue needs a plan that clarifies the finer details of how it could be implemented.schema

    Type

    No type

    Projects

    Status

    Created

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions