Skip to content

Commit

Permalink
Schema: causeFromSelf: add missing type parameter (#1974)
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti authored Jan 25, 2024
1 parent 7b84a3c commit 8d1f6e4
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/few-owls-smell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/schema": patch
---

causeFromSelf: add missing type parameter
24 changes: 24 additions & 0 deletions packages/schema/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2167,6 +2167,30 @@ In this example, we have two schemas, `schema1` and `schema2`. The first schema,

Now, by using the `compose` combinator, we can create a new schema, `composedSchema`, that combines the functionality of both `schema1` and `schema2`. This allows us to parse a string and directly obtain an array of numbers as a result.

### Non-strict Option

If you need to be less restrictive when composing your schemas, i.e., when you have something like `Schema<R1, A, B>` and `Schema<R2, C, D>` where `C` is different from `B`, you can make use of the `{ strict: false }` option:

```ts
declare const compose: <R1, A, B, R2, C, D>(
ab: Schema<R1, A, B>,
cd: Schema<R2, C, D>, // Less strict constraint
options: { strict: false }
) => Schema<R1 | R2, A, D>;
```

This is useful when you want to relax the type constraints imposed by the `decode` and `encode` functions, making them more permissive:

```ts
import * as S from "@effect/schema/Schema";

// error: Type 'string | null' is not assignable to type 'string'
S.compose(S.union(S.null, S.string), S.NumberFromString);

// ok
S.compose(S.union(S.null, S.string), S.NumberFromString, { strict: false });
```

## InstanceOf

In the following section, we demonstrate how to use the `instanceOf` combinator to create a `Schema` for a class instance.
Expand Down
22 changes: 22 additions & 0 deletions packages/schema/dtslint/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -932,3 +932,25 @@ S.head(S.array(S.number))

// $ExpectType Schema<never, readonly number[], number>
S.headOr(S.array(S.number))

// ---------------------------------------------
// cause
// ---------------------------------------------

declare const defect: S.Schema<"defect", unknown, unknown>

// $ExpectType Schema<never, CauseFrom<string>, Cause<string>>
S.cause(S.string)

// $ExpectType Schema<"defect", CauseFrom<string>, Cause<string>>
S.cause(S.string, defect)

// ---------------------------------------------
// causeFromSelf
// ---------------------------------------------

// $ExpectType Schema<never, Cause<string>, Cause<string>>
S.causeFromSelf(S.string)

// $ExpectType Schema<"defect", Cause<string>, Cause<string>>
S.causeFromSelf(S.string, defect)
8 changes: 4 additions & 4 deletions packages/schema/src/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5085,10 +5085,10 @@ const causeParse = <R, A>(
* @category Cause transformations
* @since 1.0.0
*/
export const causeFromSelf = <R, I, A>(
error: Schema<R, I, A>,
defect: Schema<never, unknown, unknown> = unknown
): Schema<R, Cause.Cause<I>, Cause.Cause<A>> => {
export const causeFromSelf = <R1, I, A, R2 = never>(
error: Schema<R1, I, A>,
defect: Schema<R2, unknown, unknown> = unknown
): Schema<R1 | R2, Cause.Cause<I>, Cause.Cause<A>> => {
return declare(
[error, defect],
(error, defect) => causeParse(ParseResult.decodeUnknown(causeFrom(error, defect))),
Expand Down

0 comments on commit 8d1f6e4

Please sign in to comment.