Skip to content

Commit

Permalink
add support for data-last subtype overloads in compose (#2631)
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Apr 26, 2024
1 parent ffe4f4e commit 8206529
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 12 deletions.
25 changes: 25 additions & 0 deletions .changeset/sixty-tables-draw.md
@@ -0,0 +1,25 @@
---
"@effect/schema": patch
---

add support for data-last subtype overloads in `compose`

Before

```ts
import { Schema as S } from "@effect/schema"

S.Union(S.Null, S.String).pipe(S.compose(S.NumberFromString)) // ts error
S.NumberFromString.pipe(S.compose(S.Union(S.Null, S.Number))) // ts error
```

Now

```ts
import { Schema as S } from "@effect/schema"

// $ExpectType Schema<number, string | null, never>
S.Union(S.Null, S.String).pipe(S.compose(S.NumberFromString)) // ok
// $ExpectType Schema<number | null, string, never>
S.NumberFromString.pipe(S.compose(S.Union(S.Null, S.Number))) // ok
```
12 changes: 6 additions & 6 deletions packages/schema/dtslint/Schema.ts
Expand Up @@ -1264,8 +1264,8 @@ S.split(",").pipe(S.compose(S.Array(S.NumberFromString)))
// $ExpectType Schema<readonly number[], string, never>
S.compose(S.split(","), S.Array(S.NumberFromString), { strict: true })

// // $ExpectType Schema<readonly number[], string, never>
// S.split(",").pipe(S.compose(S.Array(S.NumberFromString), { strict: true }))
// $ExpectType Schema<readonly number[], string, never>
S.split(",").pipe(S.compose(S.Array(S.NumberFromString), { strict: true }))

// @ts-expect-error
S.compose(S.String, S.Number)
Expand All @@ -1281,8 +1281,8 @@ S.compose(S.Union(S.Null, S.String), S.NumberFromString)
// $ExpectType Schema<number, string | null, never>
S.compose(S.Union(S.Null, S.String), S.NumberFromString, { strict: false })

// // $ExpectType Schema<number, string | null, never>
// S.Union(S.Null, S.String).pipe(S.compose(S.NumberFromString))
// $ExpectType Schema<number, string | null, never>
S.Union(S.Null, S.String).pipe(S.compose(S.NumberFromString))

// $ExpectType Schema<number, string | null, never>
S.Union(S.Null, S.String).pipe(S.compose(S.NumberFromString, { strict: false }))
Expand All @@ -1295,8 +1295,8 @@ S.compose(S.NumberFromString, S.Union(S.Null, S.Number))
// $ExpectType Schema<number | null, string, never>
S.compose(S.NumberFromString, S.Union(S.Null, S.Number), { strict: false })

// // $ExpectType Schema<number | null, string, never>
// S.NumberFromString.pipe(S.compose(S.Union(S.Null, S.Number)))
// $ExpectType Schema<number | null, string, never>
S.NumberFromString.pipe(S.compose(S.Union(S.Null, S.Number)))

// $ExpectType Schema<number | null, string, never>
S.NumberFromString.pipe(S.compose(S.Union(S.Null, S.Number), { strict: false }))
Expand Down
20 changes: 14 additions & 6 deletions packages/schema/src/Schema.ts
Expand Up @@ -2532,18 +2532,21 @@ export const extend: {
* @since 1.0.0
*/
export const compose: {
<D, C extends B, R2, B>(
to: Schema<D, C, R2>
): <A, R1>(from: Schema<B, A, R1>) => Schema<D, A, R1 | R2>
<D, C, R2>(
to: Schema<D, C, R2>
): <B extends C, A, R1>(from: Schema<B, A, R1>) => Schema<D, A, R1 | R2>
<C, B, R2>(
to: Schema<C, B, R2>
to: Schema<C, B, R2>,
options?: { readonly strict: true }
): <A, R1>(from: Schema<B, A, R1>) => Schema<C, A, R1 | R2>
<D, C, R2>(
to: Schema<D, C, R2>,
options: { readonly strict: false }
): <B, A, R1>(from: Schema<B, A, R1>) => Schema<D, A, R1 | R2>
<B, A, R1, C, R2>(
from: Schema<B, A, R1>,
to: Schema<C, B, R2>,
options?: { readonly strict: true }
): Schema<C, A, R1 | R2>

<B, A, R1, D, C extends B, R2>(
from: Schema<B, A, R1>,
to: Schema<D, C, R2>
Expand All @@ -2552,6 +2555,11 @@ export const compose: {
from: Schema<B, A, R1>,
to: Schema<D, C, R2>
): Schema<D, A, R1 | R2>
<B, A, R1, C, R2>(
from: Schema<B, A, R1>,
to: Schema<C, B, R2>,
options?: { readonly strict: true }
): Schema<C, A, R1 | R2>
<B, A, R1, D, C, R2>(
from: Schema<B, A, R1>,
to: Schema<D, C, R2>,
Expand Down

0 comments on commit 8206529

Please sign in to comment.