Skip to content

Commit 080673d

Browse files
committed
better @ts-expect-error scoping
1 parent d307e52 commit 080673d

File tree

1 file changed

+151
-74
lines changed

1 file changed

+151
-74
lines changed

Diff for: packages/openapi-fetch/test/types.test.ts

+151-74
Original file line numberDiff line numberDiff line change
@@ -20,93 +20,146 @@ describe("types", () => {
2020
test("returns all possible responses", () => {
2121
type Response = GetResponseContent<MixedResponses>;
2222
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+
});
2527
assertType<Response>("200 text/plain");
2628
assertType<Response>("206 text/plain");
2729
assertType<Response>("404 text/plain");
2830
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+
);
3135
});
3236

3337
test("returns correct type for 200 with literal", () => {
3438
type Response = GetResponseContent<MixedResponses, `${string}/${string}`, 200>;
3539
assertType<Response>({ data: "200 application/json" });
3640
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+
});
4353
});
4454

4555
test("returns correct type for 200 with json-like literal", () => {
4656
type Response = GetResponseContent<MixedResponses, `${string}/json`, 200>;
4757
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+
});
5674
});
5775

5876
test("returns correct type for 200 with application/json", () => {
5977
type Response = GetResponseContent<MixedResponses, "application/json", 200>;
6078
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+
});
6995
});
7096

7197
test("returns 200 & 500 responses", () => {
7298
type Response = GetResponseContent<MixedResponses, `${string}/${string}`, 200 | 500>;
7399
assertType<Response>({ data: "200 application/json" });
74100
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+
);
79109
assertType<Response>({ error: "500 application/json" });
80110
});
81111

82112
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+
>;
85119
assertType<Response>({ data: "200 application/json" });
86120
assertType<Response>("200 text/plain");
87121
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+
});
92130
});
93131

94132
test("non existent media type", () => {
95133
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+
);
110163
});
111164
});
112165

@@ -141,21 +194,31 @@ describe("types", () => {
141194
assertType<Response>({ data: "200 application/json" });
142195
assertType<Response>("200 text/plain");
143196
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+
});
148205
});
149206

150207
test("returns all 2XX responses, only application/json", () => {
151208
type Response = SuccessResponse<Responses, "application/json">;
152209
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+
);
159222
// @ts-expect-error
160223
assertType<Response>({ error: "500 application/json" });
161224
});
@@ -178,27 +241,41 @@ describe("types", () => {
178241

179242
test("returns all 5XX and 4xx responses", () => {
180243
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+
);
187256
assertType<Response>("404 text/plain");
188257
assertType<Response>({ error: "500 application/json" });
189258
assertType<Response>({ error: "default application/json" });
190259
});
191260

192261
test("returns all 5XX and 4xx responses, only application/json", () => {
193262
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+
);
202279
assertType<Response>({ error: "500 application/json" });
203280
assertType<Response>({ error: "default application/json" });
204281
});

0 commit comments

Comments
 (0)