File tree 1 file changed +33
-1
lines changed
1 file changed +33
-1
lines changed Original file line number Diff line number Diff line change 2
2
" effect " : minor
3
3
---
4
4
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
+ ```
You can’t perform that action at this time.
0 commit comments