Effect CommunityEC
Effect Community2y ago
66 replies
spaethnl

Creating Recursive Class-Based Schemas in TypeScript

Does anyone have any guidance on making recursive class based schemas? I have a minimal reproduction here, but I am having difficulty getting typescript to like it:

import * as S from "@effect/schema/Schema"
export type Nullable<U extends any> = U | undefined | null;

const BaseProps = { id: S.string }
type BaseProps = typeof BaseProps
interface BaseMethods { get parent(): Nullable<BaseWChildren> }

export interface BaseWChildren extends S.Class<
    BaseProps & { children: S.Schema<InstanceType<BaseWChildren>[]> },
    BaseProps & { children: S.Schema.From<BaseWChildren>[] },
    never,
    {},
    BaseWChildren,
    {},
    BaseMethods
> { }

// typescript [2506]: 'BaseWChildren' is referenced directly or indirectly in its own base expression.
export class BaseWChildren extends S.Class<BaseWChildren>()({
    ...BaseProps,
    children: S.suspend<any,any,any>(() => (BaseWChildren as any as S.Schema<any,any,any>))
}) { }
Was this page helpful?