Skip to content

Commit 2c639ec

Browse files
authoredFeb 21, 2025··
Schema: more precise return types when transformations are involved (#4487)
1 parent 8366ced commit 2c639ec

File tree

5 files changed

+2006
-1771
lines changed

5 files changed

+2006
-1771
lines changed
 

‎.changeset/real-needles-learn.md

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
---
2+
"effect": patch
3+
---
4+
5+
Schema: more precise return types when transformations are involved.
6+
7+
- `Chunk`
8+
- `NonEmptyChunk`
9+
- `Redacted`
10+
- `Option`
11+
- `OptionFromNullOr`
12+
- `OptionFromUndefinedOr`
13+
- `OptionFromNullishOr`
14+
- `Either`
15+
- `EitherFromUnion`
16+
- `ReadonlyMap`
17+
- `Map`
18+
- `HashMap`
19+
- `ReadonlySet`
20+
- `Set`
21+
- `HashSet`
22+
- `List`
23+
- `Cause`
24+
- `Exit`
25+
- `SortedSet`
26+
- `head`
27+
- `headNonEmpty`
28+
- `headOrElse`
29+
30+
**Example** (with `Schema.Chunk`)
31+
32+
Before
33+
34+
```ts
35+
import { Schema } from "effect"
36+
37+
const schema = Schema.Chunk(Schema.Number)
38+
39+
// Property 'from' does not exist on type 'Chunk<typeof Number$>'
40+
schema.from
41+
```
42+
43+
After
44+
45+
```ts
46+
import { Schema } from "effect"
47+
48+
const schema = Schema.Chunk(Schema.Number)
49+
50+
// Schema.Array$<typeof Schema.Number>
51+
schema.from
52+
```

‎packages/effect/dtslint/Schema/Schema.tst.ts

+1,588-1,425
Large diffs are not rendered by default.

‎packages/effect/src/Schema.ts

+340-336
Large diffs are not rendered by default.

‎packages/effect/test/Schema/Schema/Cause/Cause.test.ts

+6-7
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,12 @@ describe("Cause", () => {
113113
schema,
114114
Cause.die(null),
115115
`(CauseEncoded<string> <-> Cause<string>)
116-
└─ Encoded side transformation failure
117-
└─ CauseEncoded<string>
118-
└─ { readonly _tag: "Die"; readonly defect: (string <-> object) }
119-
└─ ["defect"]
120-
└─ (string <-> object)
121-
└─ Type side transformation failure
122-
└─ Expected object, actual null`
116+
└─ Type side transformation failure
117+
└─ Cause<string>
118+
└─ CauseEncoded<string>
119+
└─ { readonly _tag: "Die"; readonly defect: object }
120+
└─ ["defect"]
121+
└─ Expected object, actual null`
123122
)
124123
})
125124

‎packages/effect/test/Schema/Schema/Exit/Exit.test.ts

+20-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { describe, it } from "@effect/vitest"
2-
import { Exit } from "effect"
3-
import * as S from "effect/Schema"
2+
import { Cause, Exit, Schema as S } from "effect"
43
import * as Util from "effect/test/Schema/TestUtils"
54

65
describe("Exit", () => {
@@ -42,7 +41,25 @@ describe("Exit", () => {
4241
)
4342
})
4443

45-
it("encoding", async () => {
44+
describe("encoding", async () => {
45+
it("should raise an error when a non-encodable Cause is passed", async () => {
46+
const schema = S.Exit({ failure: S.String, success: S.Number, defect: Util.Defect })
47+
await Util.assertions.encoding.fail(
48+
schema,
49+
Exit.failCause(Cause.die(null)),
50+
`(ExitEncoded<number, string, (string <-> object)> <-> Exit<number, string>)
51+
└─ Type side transformation failure
52+
└─ Exit<number, string>
53+
└─ Cause<string>
54+
└─ CauseEncoded<string>
55+
└─ { readonly _tag: "Die"; readonly defect: object }
56+
└─ ["defect"]
57+
└─ Expected object, actual null`
58+
)
59+
})
60+
})
61+
62+
it("using the built-in Defect schema as defect argument", async () => {
4663
const schema = S.Exit({ failure: S.String, success: S.Number, defect: S.Defect })
4764
await Util.assertions.encoding.succeed(schema, Exit.fail("error"), {
4865
_tag: "Failure",

0 commit comments

Comments
 (0)
Please sign in to comment.