-
Notifications
You must be signed in to change notification settings - Fork 1
/
taggedUnion-with-common-fields.test.ts
188 lines (177 loc) · 4.08 KB
/
taggedUnion-with-common-fields.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import { expectType, TypeEqual } from "ts-expect";
import { expect, test } from "vitest";
import {
boolean,
Codec,
fields,
Infer,
InferEncoded,
number,
string,
tag,
taggedUnion,
} from "../index.js";
import { run } from "../tests/helpers.js";
test("taggedUnion with common fields", () => {
// This function takes two codecs for object types and returns
// a new codec which is the intersection of those.
// This function is not part of the tiny-decoders package because it has some caveats:
// - If either codec uses `{ allowExtraFields: false }`, it fails.
// - It’s not possible to disallow extra fields on the resulting codec.
// - It’s type wise possible to intersect with a `Record`, but what does that mean?
function intersection<
Decoded1 extends Record<string, unknown>,
Encoded1 extends Record<string, unknown>,
Decoded2 extends Record<string, unknown>,
Encoded2 extends Record<string, unknown>,
>(
codec1: Codec<Decoded1, Encoded1>,
codec2: Codec<Decoded2, Encoded2>,
): Codec<Decoded1 & Decoded2, Encoded1 & Encoded2> {
return {
decoder: (value) => {
const decoderResult1 = codec1.decoder(value);
if (decoderResult1.tag === "DecoderError") {
return decoderResult1;
}
const decoderResult2 = codec2.decoder(value);
if (decoderResult2.tag === "DecoderError") {
return decoderResult2;
}
return {
tag: "Valid",
value: {
...decoderResult1.value,
...decoderResult2.value,
},
};
},
encoder: (event) => ({
...codec1.encoder(event),
...codec2.encoder(event),
}),
};
}
type EventWithPayload = Infer<typeof EventWithPayload>;
const EventWithPayload = taggedUnion("event", [
{
event: tag("opened"),
payload: string,
},
{
event: tag("closed"),
payload: number,
},
{
event: tag("reopened", { renameTagFrom: "undo_closed" }),
payload: boolean,
},
]);
type EventMetadata = Infer<typeof EventMetadata>;
const EventMetadata = fields({
id: string,
timestamp: string,
});
type Event = Infer<typeof Event>;
const Event = intersection(EventMetadata, EventWithPayload);
expectType<
TypeEqual<
Event,
{
id: string;
timestamp: string;
} & (
| {
event: "closed";
payload: number;
}
| {
event: "opened";
payload: string;
}
| {
event: "reopened";
payload: boolean;
}
)
>
>(true);
expectType<
TypeEqual<
InferEncoded<typeof Event>,
{
id: string;
timestamp: string;
} & (
| {
event: "closed";
payload: number;
}
| {
event: "opened";
payload: string;
}
| {
event: "undo_closed";
payload: boolean;
}
)
>
>(true);
expect(
run(Event, {
id: "1",
timestamp: "2023-10-29",
event: "undo_closed",
payload: true,
}),
).toStrictEqual({
id: "1",
timestamp: "2023-10-29",
event: "reopened",
payload: true,
});
expect(
Event.encoder({
id: "1",
timestamp: "2023-10-29",
event: "reopened",
payload: true,
}),
).toStrictEqual({
id: "1",
timestamp: "2023-10-29",
event: "undo_closed",
payload: true,
});
expect(
run(Event, {
timestamp: "2023-10-29",
event: "undo_closed",
payload: true,
}),
).toMatchInlineSnapshot(`
At root:
Expected an object with a field called: "id"
Got: {
"timestamp": "2023-10-29",
"event": "undo_closed",
"payload": true
}
`);
expect(
run(Event, {
id: "1",
timestamp: "2023-10-29",
event: "other",
payload: true,
}),
).toMatchInlineSnapshot(`
At root["event"]:
Expected one of these tags:
"opened",
"closed",
"undo_closed"
Got: "other"
`);
});