Skip to content

Commit 15d4926

Browse files
authored
fix(core): invalid extraHeaders type when no headers are defined in the contract (#536)
1 parent 0b9f249 commit 15d4926

3 files changed

Lines changed: 49 additions & 3 deletions

File tree

.changeset/red-poems-guess.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@ts-rest/core': patch
3+
---
4+
5+
Fix invalid `extraHeaders` type when no headers are defined in the contract

libs/ts-rest/core/src/lib/infer-types.spec.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,29 @@ const contractStrict = c.router(contract, {
107107
strictStatusCodes: true,
108108
});
109109

110+
const headerlessContract = c.router({
111+
getPost: {
112+
method: 'GET',
113+
path: '/posts/:id',
114+
pathParams: z.object({
115+
id: z.string().transform((id) => Number(id)),
116+
}),
117+
query: z.object({
118+
includeComments: z.boolean().default(false),
119+
}),
120+
responses: {
121+
200: z.object({
122+
id: z.number(),
123+
title: z.string().default('Untitled'),
124+
content: z.string(),
125+
}),
126+
404: z.object({
127+
message: z.string(),
128+
}),
129+
},
130+
},
131+
});
132+
110133
it('type inference helpers', () => {
111134
type ServerInferResponsesTest = Expect<
112135
Equal<
@@ -556,4 +579,20 @@ it('type inference helpers', () => {
556579
}
557580
>
558581
>;
582+
583+
type ClientInferRequestHeaderlessTest = Expect<
584+
Equal<
585+
ClientInferRequest<typeof headerlessContract>,
586+
{
587+
getPost: {
588+
query: { includeComments?: boolean | undefined };
589+
params: { id: string };
590+
extraHeaders?: Record<string, string | undefined>;
591+
fetchOptions?: FetchOptions;
592+
overrideClientOptions?: Partial<OverrideableClientArgs>;
593+
cache?: RequestCache;
594+
};
595+
}
596+
>
597+
>;
559598
});

libs/ts-rest/core/src/lib/infer-types.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,11 @@ type ClientInferRequestBase<
217217
: ZodInputOrType<T['query']>
218218
: never;
219219
headers: THeaders;
220-
extraHeaders?: {
221-
[K in NonNullable<keyof THeaders>]?: never;
222-
} & Record<string, string | undefined>;
220+
extraHeaders?: [THeaders] extends [never]
221+
? Record<string, string | undefined>
222+
: {
223+
[K in NonNullable<keyof THeaders>]?: never;
224+
} & Record<string, string | undefined>;
223225
fetchOptions?: FetchOptions;
224226
overrideClientOptions?: Partial<OverrideableClientArgs>;
225227

0 commit comments

Comments
 (0)