Skip to content

Commit de92e9f

Browse files
authored
fix: make all partial args result in optional arg (#731)
<!-- Describe your PR here. --> This PR makes it so that functions that have all of their args set to optional don't have to pass an empty object to the session query to fix type errors. <!-- The following applies to third-party contributors. Convex employees and contractors can delete or ignore. --> ---- By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
2 parents f16994b + 4e186e0 commit de92e9f

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

packages/convex-helpers/react/sessions.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@ expectTypeOf<
2626
>
2727
>().toEqualTypeOf<[{ arg: string } | "skip"]>();
2828

29+
expectTypeOf<
30+
SessionQueryArgsArray<
31+
FunctionReference<
32+
"query",
33+
"public",
34+
{ arg?: string; sessionId: SessionId | null },
35+
any
36+
>
37+
>
38+
>().toEqualTypeOf<[args?: { arg?: string } | "skip"]>();
39+
2940
expectTypeOf<
3041
SessionQueryArgsArray<
3142
FunctionReference<"query", "public", { sessionId: SessionId | null }, any>
@@ -43,6 +54,17 @@ expectTypeOf<
4354
>
4455
>().toEqualTypeOf<[{ arg: string }]>();
4556

57+
expectTypeOf<
58+
SessionArgsArray<
59+
FunctionReference<
60+
"mutation",
61+
"public",
62+
{ arg?: string; sessionId: SessionId },
63+
any
64+
>
65+
>
66+
>().toEqualTypeOf<[args?: { arg?: string }]>();
67+
4668
expectTypeOf<
4769
SessionArgsArray<
4870
FunctionReference<"mutation", "public", { sessionId: SessionId }, any>

packages/convex-helpers/react/sessions.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,23 +73,33 @@ type SessionFunction<
7373
Args = any,
7474
> = FunctionReference<T, "public", { sessionId: SessionId } & Args>;
7575

76+
type ArgsWithoutSession<
77+
Fn extends SessionFunction<"query" | "mutation" | "action">,
78+
> = BetterOmit<FunctionArgs<Fn>, "sessionId">;
79+
7680
export type SessionQueryArgsArray<Fn extends SessionFunction<"query">> =
7781
keyof FunctionArgs<Fn> extends "sessionId"
7882
? [args?: EmptyObject | "skip"]
79-
: [args: BetterOmit<FunctionArgs<Fn>, "sessionId"> | "skip"];
83+
: Partial<ArgsWithoutSession<Fn>> extends ArgsWithoutSession<Fn>
84+
? [args?: ArgsWithoutSession<Fn> | "skip"]
85+
: [args: ArgsWithoutSession<Fn> | "skip"];
8086

8187
export type SessionArgsArray<
8288
Fn extends SessionFunction<"query" | "mutation" | "action">,
8389
> = keyof FunctionArgs<Fn> extends "sessionId"
8490
? [args?: EmptyObject]
85-
: [args: BetterOmit<FunctionArgs<Fn>, "sessionId">];
91+
: Partial<ArgsWithoutSession<Fn>> extends ArgsWithoutSession<Fn>
92+
? [args?: ArgsWithoutSession<Fn>]
93+
: [args: ArgsWithoutSession<Fn>];
8694

8795
export type SessionArgsAndOptions<
8896
Fn extends SessionFunction<"mutation">,
8997
Options,
9098
> = keyof FunctionArgs<Fn> extends "sessionId"
9199
? [args?: EmptyObject, options?: Options]
92-
: [args: BetterOmit<FunctionArgs<Fn>, "sessionId">, options?: Options];
100+
: Partial<ArgsWithoutSession<Fn>> extends ArgsWithoutSession<Fn>
101+
? [args?: ArgsWithoutSession<Fn>, options?: Options]
102+
: [args: ArgsWithoutSession<Fn>, options?: Options];
93103

94104
type SessionPaginatedQueryFunction<
95105
Args extends { paginationOpts: PaginationOptions } = {

0 commit comments

Comments
 (0)