Skip to content

Commit

Permalink
ensure VariantSchema preserves opaque types (#3487)
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart authored Aug 19, 2024
1 parent 35be739 commit 00670d0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/brave-bobcats-enjoy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/experimental": patch
---

ensure VariantSchema preserves opaque types
13 changes: 8 additions & 5 deletions packages/experimental/src/VariantSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,12 @@ export declare namespace Field {
* @since 1.0.0
* @category extractors
*/
export type ExtractFields<V extends string, Fields extends Struct.Fields> = {
export type ExtractFields<V extends string, Fields extends Struct.Fields, IsDefault = false> = {
readonly [
K in keyof Fields as [Fields[K]] extends [Field<infer Config>] ? V extends keyof Config ? K
: never
: K
]: [Fields[K]] extends [Struct<infer _>] ? Extract<V, Fields[K]>
]: [Fields[K]] extends [Struct<infer _>] ? Extract<V, Fields[K], IsDefault>
: [Fields[K]] extends [Field<infer Config>]
? [Config[V]] extends [Schema.Schema.All | Schema.PropertySignature.All] ? Config[V]
: never
Expand All @@ -145,9 +145,12 @@ export type ExtractFields<V extends string, Fields extends Struct.Fields> = {
* @since 1.0.0
* @category extractors
*/
export type Extract<V extends string, A extends Struct<any>> = [A] extends [
export type Extract<V extends string, A extends Struct<any>, IsDefault = false> = [A] extends [
Struct<infer Fields>
] ? Schema.Struct<Schema.Simplify<ExtractFields<V, Fields>>>
] ?
IsDefault extends true
? [A] extends [Schema.Schema.Any] ? A : Schema.Struct<Schema.Simplify<ExtractFields<V, Fields>>>
: Schema.Struct<Schema.Simplify<ExtractFields<V, Fields>>>
: never

/**
Expand Down Expand Up @@ -318,7 +321,7 @@ export const make = <
& ClassFromFields<
Self,
Fields,
Schema.Simplify<ExtractFields<Default, Fields>>
Schema.Simplify<ExtractFields<Default, Fields, true>>
>
& {
readonly [V in Variants[number]]: Extract<V, Struct<Fields>>
Expand Down

0 comments on commit 00670d0

Please sign in to comment.