Effect CommunityEC
Effect Community16mo ago
3 replies
Silvestro

Defining a Struct Field Based on Union of Other Structs in Effect/Schema

Hi everyone, I am new to use effect/schema. I am trying to define one field of a Struct based on the field of the Union of others Struct.

I need to have the union of the different name to be "a" | "b" but the code below returns me S.SchemaClass<{name: "a" | "b"}>

Here's the code

import {Schema as S} from "@effect/schema"

const A = S.Struct({
  name: S.Literal("a"),
  other: S.NullOr(S.String)
})

const B = S.Struct({
  name: S.Literal("b"),
  other: S.NullOr(S.String)
})

type AB = typeof AB.Type
const AB = S.Union(A, B)

const C = S.Struct({
  id: AB.pipe(S.pick("name")) // Here's the problem: id should be "a" | "b" and not {name: "a" | "b"}  
  // ...other fields
})
Was this page helpful?