Skip to content
This repository was archived by the owner on Oct 26, 2026. It is now read-only.

Commit 9bd4c77

Browse files
authored
fix: broken typing for optional arguments (#183)
1 parent 891b6ed commit 9bd4c77

4 files changed

Lines changed: 91 additions & 4 deletions

File tree

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+
Fixes broken typing where sometimes the client's request args were incorrectly typed as optional

‎libs/ts-rest/core/src/lib/client.spec.ts‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as fetchMock from 'fetch-mock-jest';
22
import { initContract } from '..';
33
import { ApiFetcherArgs, initClient } from './client';
4+
import { Equal, Expect } from './test-helpers';
45

56
import { z } from 'zod';
67

@@ -124,6 +125,33 @@ const client = initClient(router, {
124125
baseHeaders: {},
125126
});
126127

128+
type ClientGetPostsType = Expect<
129+
Equal<
130+
Parameters<typeof client.posts.getPosts>[0],
131+
| {
132+
query?: {
133+
take?: number;
134+
skip?: number;
135+
order?: string;
136+
};
137+
headers?: Record<string, string>;
138+
}
139+
| undefined
140+
>
141+
>;
142+
143+
type ClientGetPostType = Expect<
144+
Equal<
145+
Parameters<typeof client.posts.getPost>[0],
146+
{
147+
params: {
148+
id: string;
149+
};
150+
headers?: Record<string, string>;
151+
}
152+
>
153+
>;
154+
127155
describe('client', () => {
128156
beforeEach(() => {
129157
fetchMock.mockReset();
@@ -497,6 +525,35 @@ const customClient = initClient(router, {
497525
},
498526
});
499527

528+
type CustomClientGetPostsType = Expect<
529+
Equal<
530+
Parameters<typeof customClient.posts.getPosts>[0],
531+
| {
532+
query?: {
533+
take?: number;
534+
skip?: number;
535+
order?: string;
536+
};
537+
headers?: Record<string, string>;
538+
uploadProgress?: (progress: number) => void;
539+
}
540+
| undefined
541+
>
542+
>;
543+
544+
type CustomClientGetPostType = Expect<
545+
Equal<
546+
Parameters<typeof customClient.posts.getPost>[0],
547+
{
548+
params: {
549+
id: string;
550+
};
551+
headers?: Record<string, string>;
552+
uploadProgress?: (progress: number) => void;
553+
}
554+
>
555+
>;
556+
500557
describe('custom api', () => {
501558
beforeEach(() => {
502559
argsCalledMock.mockReset();

‎libs/ts-rest/core/src/lib/type-utils.spec.ts‎

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
/* eslint-disable @typescript-eslint/no-unused-vars */
22
import { z } from 'zod';
33
import { Equal, Expect } from './test-helpers';
4-
import { AreAllPropertiesOptional, ZodInferOrType } from './type-utils';
4+
import {
5+
AreAllPropertiesOptional,
6+
Without,
7+
ZodInferOrType,
8+
} from './type-utils';
59

610
const zodObject = z.object({ title: z.string() });
711
type Test1 = ZodInferOrType<typeof zodObject>;
@@ -27,8 +31,29 @@ type AreAllPropertiesOptional3 = Expect<
2731
params: {
2832
id: string;
2933
};
30-
headers?: Record<string, string> | undefined;
34+
headers?: Record<string, string>;
3135
}>,
3236
false
3337
>
3438
>;
39+
40+
type WithoutTest = Expect<
41+
Equal<
42+
Without<
43+
{
44+
body: never;
45+
params: {
46+
id: string;
47+
};
48+
headers?: Record<string, string>;
49+
},
50+
never
51+
>,
52+
{
53+
params: {
54+
id: string;
55+
};
56+
headers?: Record<string, string>;
57+
}
58+
>
59+
>;

‎libs/ts-rest/core/src/lib/type-utils.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ export type GetFieldType<T, P> = P extends `${infer Left}.${infer Right}`
4040
// https://stackoverflow.com/questions/63447660/typescript-remove-all-properties-with-particular-type
4141
// Nested solution also available ^
4242
type ExcludeKeysWithTypeOf<T, V> = {
43-
[K in keyof T]: Exclude<T[K], undefined> extends V ? never : K;
43+
[K in keyof T]-?: Exclude<T[K], undefined> extends V ? never : K;
4444
}[keyof T];
4545

4646
type ExcludeKeysWithoutTypeOf<T, V> = {
47-
[K in keyof T]: Exclude<T[K], undefined> extends V ? K : never;
47+
[K in keyof T]-?: Exclude<T[K], undefined> extends V ? K : never;
4848
}[keyof T];
4949

5050
export type Without<T, V> = Pick<T, ExcludeKeysWithTypeOf<T, V>>;

0 commit comments

Comments
 (0)