Metakit / Write a rule once

5. Write a generic helper over a Schema

When you write a generic helper that both builds a wrapper Schema (Schema.Struct, Schema.Array, Expr.refine, Inspect.fieldSpecs) and decodes it synchronously (Schema.decodeUnknownSync), constrain the type parameter to SchemaOf (Schema.Top & Schema.ConstraintDecoder<unknown>) exported from @doeixd/metakit:

ts
import { Schema } from 'effect'
import type { SchemaOf } from '@doeixd/metakit'

export const decodeTable = <S extends SchemaOf>(row: S, input: unknown) =>
  Schema.decodeUnknownSync(Schema.Struct({ rows: Schema.Array(row) }))(input)

Using only Schema.Top fails on decodeUnknownSync (DecodingServices is unknown, not never); using only Schema.ConstraintDecoder<unknown> fails on Schema.Struct / Schema.Array because it lacks Schema.Top's combinators. When your helper only constructs or inspects Schemas (without decoding), Schema.Top alone is enough.

If you attach Schema.annotate({ title, description }) alongside Expr.refine(...) and also pass the Schema to Effect's Schema.toJsonSchemaDocument, place .annotate(...) before Expr.refine(...) (@doeixd/metakit/inspect reads annotations in either order, while Effect's generator reads annotations placed before the refinement).

Next