Effect CommunityEC
Effect Community2y ago
6 replies
sign error

Accessing Union Type Schema in TypeScript

Hey there! Assume that Schema;

const UnionFromString = S.transformOrFail(
  S.String,
  S.Union(
    S.Struct({ 
      type: S.Literal('a'),
      a: S.Number
    }), 
    S.Struct({ 
      type: S.Literal('b'),
      b: S.String
    })
  ),
  {
    // Question here
    decode: str => {
      const type = str.slice(0,1);
      // How can use the Unions Schema for the »type«
      // property here in order decode it
    },
    encode: item => …
  }
)


How can I access / »derive« the Schema for the Unions »type« Property?
Was this page helpful?