Effect CommunityEC
Effect Community2y ago
34 replies
Stephen Bluck

Correct Way to Write a Transformation in TypeScript Using @effect/schema

Is this the correct way to write a transformation?
import { Schema } from "@effect/schema"
import type { OptionEncoded } from "@effect/schema/Schema"

export const OptionFromEmptyString = Schema.transform(Schema.String, Schema.Option(Schema.NonEmpty), {
  decode: (value): OptionEncoded<string> => (value.length > 0 ? { _tag: "Some", value } : { _tag: "None" }),
  encode: (opt) => (opt._tag === "Some" ? opt.value : "")
})

Am I right in thinking we always must map to the encoded version of the from/to schema's?
Was this page helpful?