Handling Date and String Conversion with Effect Schema

I feel like I may have hit a limitation with Effect schema or I am just not doing this correctly. Essentially I am trying to have a Schema which takes a Date | string -> decode -> Utc -> encode -> Date

BUT what I get from the Date | string -> decode -> Utc -> encode -> Date | string (which makes sense)
https://effect.website/play/#19db4f36eb35 - I hit a snag which was the Encoded type will be Date | string (assuming this is how the transformOrFail infers). When in fact, the value itself will ALWAYS be a Date.

If what I am trying to do isn't possible, is there a way to force this to Encoded TS type to a Date somehow inside the transformOrFail method 🤔

---

But basically looks like this:

export class DateTimeUtcFromDateOrStringBackToDate extends S.transformOrFail(
  S.Union(
    DateFromSelf,
    DateTimeUtc
    // Thought maybe here you could force Encoded type to always be a Date
    // .pipe(
    //   S.asSchema(DateFromSelf.)
    // ),
  ),
  DateTimeUtcFromSelf,
  {
    strict: true,
    decode: decodeDateTimeUtc,
    encode: (dt) => ParseResult.succeed<Date>(DateTime.toDateUtc(dt) as Date),
  },
).annotations({ identifier: "DateTimeUtcFromDateOrStringBackToDate" }) {}
Was this page helpful?