Skip to content

Commit 81431c8

Browse files
committed
fix: inference of ResponseType<HttpRoute>
Instead of `never` this should be something like the union of all responses with `any` or `unknown` body.
1 parent ff78c84 commit 81431c8

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

packages/io-ts-http/src/httpResponse.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ import * as t from 'io-ts';
22

33
import { Status } from '@api-ts/response';
44

5-
export type HttpResponse = t.Props;
5+
export type HttpResponse = {
6+
[K: string]: t.Mixed;
7+
};
68

7-
export type KnownResponses<Response extends HttpResponse> = {
8-
[K in keyof Response]: K extends Status ? K : never;
9-
}[keyof Response];
9+
type KnownResponses<Response extends HttpResponse> = {
10+
[K in Status]: K extends keyof Response ? K : never;
11+
}[Status];
1012

1113
export const HttpResponseCodes: { [K in Status]: number } = {
1214
ok: 200,

packages/io-ts-http/src/httpRoute.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import * as t from 'io-ts';
22

3-
import { HttpResponse, KnownResponses } from './httpResponse';
3+
import { HttpResponse } from './httpResponse';
44
import { HttpRequestCodec } from './httpRequest';
5+
import { Status } from '@api-ts/response';
56

67
export type Method = 'GET' | 'POST' | 'PUT' | 'DELETE';
78

@@ -14,11 +15,13 @@ export type HttpRoute = {
1415

1516
export type RequestType<T extends HttpRoute> = t.TypeOf<T['request']>;
1617
export type ResponseType<T extends HttpRoute> = {
17-
[K in KnownResponses<T['response']>]: {
18-
type: K;
19-
payload: t.TypeOf<T['response'][K]>;
20-
};
21-
}[KnownResponses<T['response']>];
18+
[K in Status]: K extends keyof T['response']
19+
? {
20+
type: K;
21+
payload: t.TypeOf<T['response'][K]>;
22+
}
23+
: never;
24+
}[Status];
2225

2326
export type ApiSpec = {
2427
[Key: string]: {

0 commit comments

Comments
 (0)