Skip to content

Fix effect-schema $type output inference#5513

Open
danieljvdm wants to merge 5 commits intodrizzle-team:betafrom
danieljvdm:fix/effect-schema-type-override
Open

Fix effect-schema $type output inference#5513
danieljvdm wants to merge 5 commits intodrizzle-team:betafrom
danieljvdm:fix/effect-schema-type-override

Conversation

@danieljvdm
Copy link

@danieljvdm danieljvdm commented Mar 19, 2026

effect-schema picked the runtime schema from the SQL column kind, but it dropped TColumn["_"]["data"] from the exposed Effect type. In emitted consumer types that could widen .$type<>() overrides back to the base schema output, and a plain Schema<TData, ...> rewrite also risked erasing schema-specific surface.

Broken:

type MyBrandedId = string & Brand.Brand<"MyBrandedId">;
type Foo = s.Schema.Type<typeof s.Literal("Foo")>;

const table = pgTable("test", {
  id: uuid().$type<MyBrandedId>().notNull(),
  value: varchar().$type<Foo>().notNull(),
});

const result = createSelectSchema(table);
// Schema.Type<typeof result>["id"] was string
// Schema.Type<typeof result.fields.id> could also widen unexpectedly
// Schema.Context<typeof result.fields.id> needed to stay never

Now working:

type RebuildEffectSchema<TSchema extends Schema.Any, TData> =
  Omit<TSchema, keyof Schema.Any> &
  Schema<TData, s.Schema.Encoded<TSchema>, s.Schema.Context<TSchema>>;

GetEffectSchemaType now captures TData once at the export boundary and applies the override by rebuilding the schema structurally, so it preserves the original schema surface while replacing only the exposed output type.

Also added PG validator assertions for:

  • Schema.Type<typeof result>[...]
  • Schema.Type<typeof result.fields....>
  • Schema.Context<typeof result>
  • Schema.Context<typeof result.fields....>

@danieljvdm danieljvdm force-pushed the fix/effect-schema-type-override branch from 0e43336 to 8d07e8b Compare March 19, 2026 19:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant