### Elegant Pattern Matching for Union Schema in TypeScript

Considering a union schema (being discriminated or not) such as:
const FooInput = Struct({
  foo: String
})

const BarInput = Struct({
  bar: String
})

const BazInput = Struct({
  baz: String
})

const Input = Union(
  FooInput.pipe(attachPropertySignature("type", "foo")),
  BarInput.pipe(attachPropertySignature("type", "bar")),
  BazInput.pipe(attachPropertySignature("type", "baz"))
)


What's the best/recommended way to do some stuff based on which input is passed to the function ?

Because doing things such as:
if (Schema.is(FooInput)(input)) {
  return yield* doStuffA();
}

if (Schema.is(BarInput)(input)) {
  return yield* doStuffB();
}

if (Schema.is(BazInput)(input)) {
  return yield* doStuffC();
}

(or with a switch statement should work fine but does not feel very elegant)

I started to read about the pattern matching guide on the effect website. However the thing I missed is why the different output paths (I don't know the right term for this) expect non-effect functions:
const whenTypeIs = Match.discriminator("type");
Match.value(command).pipe(
  whenTypeIs("foo", (_) => doStuffA()),
  whenTypeIs("bar", (_) => doStuffB()),
  whenTypeIs("baz", (_) => doStuffC()),
  Match.exhaustive,
);


Because I want to yield* some effect and I cannot seem to find the proper way to do with Match.
Was this page helpful?