Effect CommunityEC
Effect Community16mo ago
5 replies
Josh

Handling Nested Optional Fields in TypeScript Schema Transformations

I'm struggling to transform when dealing with a nested optional field. I'd like to do something like this:
const attempt = Schema.transform(
  Schema.Struct({
    foo: Schema.Struct({
      bar: Schema.optionalWith(Schema.String, { as: 'Option', nullable: true }),
    }),
  }),
  Schema.Option(Schema.String),
  {
    strict: true,
    encode: (value) => ({ foo: { bar: Option.getOrNull(value) } }),
    decode: (value) => value.foo.bar,
  },
)


but types aren't happy because Schema.OptionEncoded isn't compatible with Option. Not sure what else I could use as the second arg to transform since propertySignatures aren't valid there
Was this page helpful?