@@ -20,93 +20,146 @@ describe("types", () => {
20
20
test ( "returns all possible responses" , ( ) => {
21
21
type Response = GetResponseContent < MixedResponses > ;
22
22
assertType < Response > ( { data : "200 application/json" } ) ;
23
- // @ts -expect-error It picks literal over string
24
- assertType < Response > ( { data : "200 but different string" } ) ;
23
+ assertType < Response > ( {
24
+ // @ts -expect-error It picks literal over string
25
+ data : "200 but different string" ,
26
+ } ) ;
25
27
assertType < Response > ( "200 text/plain" ) ;
26
28
assertType < Response > ( "206 text/plain" ) ;
27
29
assertType < Response > ( "404 text/plain" ) ;
28
30
assertType < Response > ( { error : "500 application/json" } ) ;
29
- // @ts -expect-error 204 never does not become undefined
30
- assertType < Response > ( undefined ) ;
31
+ assertType < Response > (
32
+ // @ts -expect-error 204 never does not become undefined
33
+ undefined ,
34
+ ) ;
31
35
} ) ;
32
36
33
37
test ( "returns correct type for 200 with literal" , ( ) => {
34
38
type Response = GetResponseContent < MixedResponses , `${string } /${string } `, 200 > ;
35
39
assertType < Response > ( { data : "200 application/json" } ) ;
36
40
assertType < Response > ( "200 text/plain" ) ;
37
- // @ts -expect-error
38
- assertType < Response > ( "206 text/plain" ) ;
39
- // @ts -expect-error
40
- assertType < Response > ( "404 text/plain" ) ;
41
- // @ts -expect-error
42
- assertType < Response > ( { error : "500 application/json" } ) ;
41
+ assertType < Response > (
42
+ // @ts -expect-error
43
+ "206 text/plain" ,
44
+ ) ;
45
+ assertType < Response > (
46
+ // @ts -expect-error
47
+ "404 text/plain" ,
48
+ ) ;
49
+ assertType < Response > ( {
50
+ // @ts -expect-error
51
+ error : "500 application/json" ,
52
+ } ) ;
43
53
} ) ;
44
54
45
55
test ( "returns correct type for 200 with json-like literal" , ( ) => {
46
56
type Response = GetResponseContent < MixedResponses , `${string } /json`, 200 > ;
47
57
assertType < Response > ( { data : "200 application/json" } ) ;
48
- // @ts -expect-error
49
- assertType < Response > ( "200 text/plain" ) ;
50
- // @ts -expect-error
51
- assertType < Response > ( "206 text/plain" ) ;
52
- // @ts -expect-error
53
- assertType < Response > ( "404 text/plain" ) ;
54
- // @ts -expect-error
55
- assertType < Response > ( { error : "500 application/json" } ) ;
58
+ assertType < Response > (
59
+ // @ts -expect-error
60
+ "200 text/plain" ,
61
+ ) ;
62
+ assertType < Response > (
63
+ // @ts -expect-error
64
+ "206 text/plain" ,
65
+ ) ;
66
+ assertType < Response > (
67
+ // @ts -expect-error
68
+ "404 text/plain" ,
69
+ ) ;
70
+ assertType < Response > ( {
71
+ // @ts -expect-error
72
+ error : "500 application/json" ,
73
+ } ) ;
56
74
} ) ;
57
75
58
76
test ( "returns correct type for 200 with application/json" , ( ) => {
59
77
type Response = GetResponseContent < MixedResponses , "application/json" , 200 > ;
60
78
assertType < Response > ( { data : "200 application/json" } ) ;
61
- // @ts -expect-error
62
- assertType < Response > ( "200 text/plain" ) ;
63
- // @ts -expect-error
64
- assertType < Response > ( "206 text/plain" ) ;
65
- // @ts -expect-error
66
- assertType < Response > ( "404 text/plain" ) ;
67
- // @ts -expect-error
68
- assertType < Response > ( { error : "500 application/json" } ) ;
79
+ assertType < Response > (
80
+ // @ts -expect-error
81
+ "200 text/plain" ,
82
+ ) ;
83
+ assertType < Response > (
84
+ // @ts -expect-error
85
+ "206 text/plain" ,
86
+ ) ;
87
+ assertType < Response > (
88
+ // @ts -expect-error
89
+ "404 text/plain" ,
90
+ ) ;
91
+ assertType < Response > ( {
92
+ // @ts -expect-error
93
+ error : "500 application/json" ,
94
+ } ) ;
69
95
} ) ;
70
96
71
97
test ( "returns 200 & 500 responses" , ( ) => {
72
98
type Response = GetResponseContent < MixedResponses , `${string } /${string } `, 200 | 500 > ;
73
99
assertType < Response > ( { data : "200 application/json" } ) ;
74
100
assertType < Response > ( "200 text/plain" ) ;
75
- // @ts -expect-error
76
- assertType < Response > ( "206 text/plain" ) ;
77
- // @ts -expect-error
78
- assertType < Response > ( "404 text/plain" ) ;
101
+ assertType < Response > (
102
+ // @ts -expect-error
103
+ "206 text/plain" ,
104
+ ) ;
105
+ assertType < Response > (
106
+ // @ts -expect-error
107
+ "404 text/plain" ,
108
+ ) ;
79
109
assertType < Response > ( { error : "500 application/json" } ) ;
80
110
} ) ;
81
111
82
112
test ( "returns all OK responses" , ( ) => {
83
- // @ts -expect-error: Type 'OkStatus' does not satisfy the constraint 'keyof MixedResponses'. Can safely ignore this error.
84
- type Response = GetResponseContent < MixedResponses , `${string } /${string } `, OkStatus > ;
113
+ type Response = GetResponseContent <
114
+ MixedResponses ,
115
+ `${string } /${string } `,
116
+ // @ts -expect-error: Type 'OkStatus' does not satisfy the constraint 'keyof MixedResponses'. Can safely ignore this error.
117
+ OkStatus
118
+ > ;
85
119
assertType < Response > ( { data : "200 application/json" } ) ;
86
120
assertType < Response > ( "200 text/plain" ) ;
87
121
assertType < Response > ( "206 text/plain" ) ;
88
- // @ts -expect-error
89
- assertType < Response > ( "404 text/plain" ) ;
90
- // @ts -expect-error
91
- assertType < Response > ( { error : "500 application/json" } ) ;
122
+ assertType < Response > (
123
+ // @ts -expect-error
124
+ "404 text/plain" ,
125
+ ) ;
126
+ assertType < Response > ( {
127
+ // @ts -expect-error
128
+ error : "500 application/json" ,
129
+ } ) ;
92
130
} ) ;
93
131
94
132
test ( "non existent media type" , ( ) => {
95
133
type Response = GetResponseContent < MixedResponses , "I/DO NOT EXIST" > ;
96
- // @ts -expect-error
97
- assertType < Response > ( { data : "200 application/json" } ) ;
98
- // @ts -expect-error
99
- assertType < Response > ( { data : "200 but different string" } ) ;
100
- // @ts -expect-error
101
- assertType < Response > ( "200 text/plain" ) ;
102
- // @ts -expect-error
103
- assertType < Response > ( "206 text/plain" ) ;
104
- // @ts -expect-error
105
- assertType < Response > ( "404 text/plain" ) ;
106
- // @ts -expect-error
107
- assertType < Response > ( { error : "500 application/json" } ) ;
108
- // @ts -expect-error 204 never does not become undefined
109
- assertType < Response > ( undefined ) ;
134
+ assertType < Response > ( {
135
+ // @ts -expect-error
136
+ data : "200 application/json" ,
137
+ } ) ;
138
+ assertType < Response > ( {
139
+ // @ts -expect-error
140
+ data : "200 but different string" ,
141
+ } ) ;
142
+ assertType < Response > (
143
+ // @ts -expect-error
144
+ "200 text/plain" ,
145
+ ) ;
146
+ assertType < Response > (
147
+ // @ts -expect-error
148
+ "206 text/plain" ,
149
+ ) ;
150
+ assertType < Response > (
151
+ // @ts -expect-error
152
+ "404 text/plain" ,
153
+ ) ;
154
+
155
+ assertType < Response > ( {
156
+ // @ts -expect-error
157
+ error : "500 application/json" ,
158
+ } ) ;
159
+ assertType < Response > (
160
+ // @ts -expect-error 204 never does not become undefined
161
+ undefined ,
162
+ ) ;
110
163
} ) ;
111
164
} ) ;
112
165
@@ -141,21 +194,31 @@ describe("types", () => {
141
194
assertType < Response > ( { data : "200 application/json" } ) ;
142
195
assertType < Response > ( "200 text/plain" ) ;
143
196
assertType < Response > ( "206 text/plain" ) ;
144
- // @ts -expect-error
145
- assertType < Response > ( "404 text/plain" ) ;
146
- // @ts -expect-error
147
- assertType < Response > ( { error : "500 application/json" } ) ;
197
+ assertType < Response > (
198
+ // @ts -expect-error
199
+ "404 text/plain" ,
200
+ ) ;
201
+ assertType < Response > ( {
202
+ // @ts -expect-error
203
+ error : "500 application/json" ,
204
+ } ) ;
148
205
} ) ;
149
206
150
207
test ( "returns all 2XX responses, only application/json" , ( ) => {
151
208
type Response = SuccessResponse < Responses , "application/json" > ;
152
209
assertType < Response > ( { data : "200 application/json" } ) ;
153
- // @ts -expect-error
154
- assertType < Response > ( "200 text/plain" ) ;
155
- // @ts -expect-error
156
- assertType < Response > ( "206 text/plain" ) ;
157
- // @ts -expect-error
158
- assertType < Response > ( "404 text/plain" ) ;
210
+ assertType < Response > (
211
+ // @ts -expect-error
212
+ "200 text/plain" ,
213
+ ) ;
214
+ assertType < Response > (
215
+ // @ts -expect-error
216
+ "206 text/plain" ,
217
+ ) ;
218
+ assertType < Response > (
219
+ // @ts -expect-error
220
+ "404 text/plain" ,
221
+ ) ;
159
222
// @ts -expect-error
160
223
assertType < Response > ( { error : "500 application/json" } ) ;
161
224
} ) ;
@@ -178,27 +241,41 @@ describe("types", () => {
178
241
179
242
test ( "returns all 5XX and 4xx responses" , ( ) => {
180
243
type Response = ErrorResponse < Responses > ;
181
- // @ts -expect-error
182
- assertType < Response > ( { data : "200 application/json" } ) ;
183
- // @ts -expect-error
184
- assertType < Response > ( "200 text/plain" ) ;
185
- // @ts -expect-error
186
- assertType < Response > ( "206 text/plain" ) ;
244
+ assertType < Response > ( {
245
+ // @ts -expect-error
246
+ data : "200 application/json" ,
247
+ } ) ;
248
+ assertType < Response > (
249
+ // @ts -expect-error
250
+ "200 text/plain" ,
251
+ ) ;
252
+ assertType < Response > (
253
+ // @ts -expect-error
254
+ "206 text/plain" ,
255
+ ) ;
187
256
assertType < Response > ( "404 text/plain" ) ;
188
257
assertType < Response > ( { error : "500 application/json" } ) ;
189
258
assertType < Response > ( { error : "default application/json" } ) ;
190
259
} ) ;
191
260
192
261
test ( "returns all 5XX and 4xx responses, only application/json" , ( ) => {
193
262
type Response = ErrorResponse < Responses , "application/json" > ;
194
- // @ts -expect-error
195
- assertType < Response > ( { data : "200 application/json" } ) ;
196
- // @ts -expect-error
197
- assertType < Response > ( "200 text/plain" ) ;
198
- // @ts -expect-error
199
- assertType < Response > ( "206 text/plain" ) ;
200
- // @ts -expect-error
201
- assertType < Response > ( "404 text/plain" ) ;
263
+ assertType < Response > ( {
264
+ // @ts -expect-error
265
+ data : "200 application/json" ,
266
+ } ) ;
267
+ assertType < Response > (
268
+ // @ts -expect-error
269
+ "200 text/plain" ,
270
+ ) ;
271
+ assertType < Response > (
272
+ // @ts -expect-error
273
+ "206 text/plain" ,
274
+ ) ;
275
+ assertType < Response > (
276
+ // @ts -expect-error
277
+ "404 text/plain" ,
278
+ ) ;
202
279
assertType < Response > ( { error : "500 application/json" } ) ;
203
280
assertType < Response > ( { error : "default application/json" } ) ;
204
281
} ) ;
0 commit comments