Effect CommunityEC
Effect Community2y ago
23 replies
spaethnl

Reference Parent Schema in Child Schema

Is there a good way to construct a self-referential schema?

interface ParentChildEncoded { data: string, child?: ParentChild | undefined }
interface ParentChildType extends ParentChildEncoded { parent?: ParentChild }
type ParentChild$ = S.Schema<ParentChildType, ParentChildEncoded>
const fields = {
  data: S.String,
  child: S.optional(S.suspend((): ParentChild$ => ParentChild))
}
class ParentChild extends S.Class<ParentChild>('ParentChild')(fields) {
  readonly parent?: ParentChild
}

const parent = new ParentChild({
  data: 'The parent',
  child: {
    data: 'The Child',
    parent: () => parent // <-- Need a thunk here?
  }
}) 


I could implement a custom constructor, but I am hoping I am just missing a built-in mechanism.
Was this page helpful?