Skip to content

Commit efef02f

Browse files
tim-smarteffect-bot
authored andcommitted
update changeset
1 parent b058df9 commit efef02f

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

.changeset/clean-bobcats-matter.md

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,36 @@
22
"effect": minor
33
---
44

5-
Schema: add `taggedUnion` to create discriminiated unions
5+
add `Schema.TaggedUnion` to create discriminiated unions
6+
7+
This allows you to quickly create a serialiable discriminated union with
8+
`_tag`'s for each case.
9+
10+
```ts
11+
import { Schema } from "effect"
12+
import { strictEqual } from "node:assert"
13+
14+
export const HttpError = Schema.TaggedUnion({
15+
BadRequest: {
16+
status: Schema.tag(400),
17+
message: Schema.String
18+
},
19+
NotFound: {
20+
status: Schema.tag(404),
21+
message: Schema.String
22+
}
23+
})
24+
25+
// access the member schemas
26+
HttpError.members.BadRequest
27+
28+
// create an instance of a member
29+
const error = HttpError.members.NotFound.make({ message: "Not Found" })
30+
31+
// use the provided $match helper to perform pattern matching
32+
const statusCode = HttpError.$match(error, {
33+
BadRequest: (e) => e.status,
34+
NotFound: (e) => e.status
35+
})
36+
strictEqual(statusCode, 404)
37+
```

0 commit comments

Comments
 (0)