Effect CommunityEC
Effect Community2y ago
6 replies
kurokumori

Resolving TypeScript Errors in Recursive Schema Definition with Branded Types

Hello, I've used "A Helpful Pattern to Simplify Schema Definition" for recursive scheme definition from docs and got typescript complains about Union and Brand
The error is "Type 'string' is not assignable to type 'Brand<"Branded">"
import * as S from "@effect/schema/Schema";

const Branded = S.String.pipe(S.brand("Branded"));
type Branded = typeof Branded.Type;

const fields = { id: Branded }

interface Entry extends S.Struct.Type<typeof fields> {
    readonly children: ReadonlyArray<UnionItem>
}

export const Entry = S.Struct({
    ...fields,
    children: S.Array(S.suspend((): S.Schema<UnionItem> => UnionItem)),
});

type UnionItem = Entry | number;
const UnionItem = S.Union(Entry, S.Number);
Was this page helpful?