Effect CommunityEC
Effect Community2y ago
6 replies
Arman

Transforming and Composing Schemas with Property Name Changes in TypeScript

Changing a property name between input/output of a schema and composing this with another struct (please refer me to another issue/docs if this is described else where)
My current attempt:
import { ParseResult, Schema as S } from '@effect/schema'

const A = S.Struct({
  a: S.String,
})

const B = S.Struct({
  b: S.String,
})

const MySchema = S.transformOrFail(
  A,
  B,
  {
    decode: ({ a }) => ParseResult.succeed({ b: a }),
    encode: ({ b }) => ParseResult.succeed({ a: b }),
  },
)

const FullSchema = S
  .Struct({
    c: S.String,
  })
  .pipe(S.extend(MySchema))

// try it
console.log(S.decodeSync(FullSchema)({ c: 'c', a: 'a' }))
console.log(S.encodeSync(FullSchema)({ c: 'c', b: 'b' }))


This leads to
Error: Unsupported schema or overlapping types
details: cannot extend { readonly c: string } with ({ readonly a: string } <-> { readonly b: string })

But I don't quite understand why. The props are not overlapping. Is there a different way to accomplish this composition? (basically changing a prop name, in this case its string -> string but this can be any -> any schema too under a different prop name)
Was this page helpful?