Skip to content

types(OptionalRestArgs): truly only require args input when necessary #59

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/react/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
getFunctionName,
makeFunctionReference,
} from "../server/api.js";
import { EmptyObject } from "../server/registration.js";
import { AreAllPropertiesOptional } from "../server/registration.js";
import {
instantiateDefaultLogger,
instantiateNoopLogger,
Expand Down Expand Up @@ -606,9 +606,9 @@ export const ConvexProvider: React.FC<{
};

export type OptionalRestArgsOrSkip<FuncRef extends FunctionReference<any>> =
FuncRef["_args"] extends EmptyObject
? [args?: EmptyObject | "skip"]
: [args: FuncRef["_args"] | "skip"];
AreAllPropertiesOptional<FuncRef['_args']> extends true
? [args?: FuncRef['_args'] | "skip"]
: [args: FuncRef['_args'] | "skip"];

/**
* Load a reactive query within a React component.
Expand Down
14 changes: 8 additions & 6 deletions src/server/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
RegisteredAction,
RegisteredMutation,
RegisteredQuery,
AreAllPropertiesOptional,
} from "./registration.js";
import { Expand, UnionToIntersection } from "../type_utils.js";
import { PaginationOptions, PaginationResult } from "./pagination.js";
Expand Down Expand Up @@ -444,9 +445,9 @@ export type FunctionArgs<FuncRef extends AnyFunctionReference> =
* @public
*/
export type OptionalRestArgs<FuncRef extends AnyFunctionReference> =
FuncRef["_args"] extends EmptyObject
? [args?: EmptyObject]
: [args: FuncRef["_args"]];
AreAllPropertiesOptional<FuncRef['_args']> extends true
? [args?: FuncRef['_args']]
: [args: FuncRef['_args']]

/**
* A tuple type of the (maybe optional) arguments to `FuncRef`, followed by an options
Expand All @@ -460,9 +461,10 @@ export type OptionalRestArgs<FuncRef extends AnyFunctionReference> =
export type ArgsAndOptions<
FuncRef extends AnyFunctionReference,
Options,
> = FuncRef["_args"] extends EmptyObject
? [args?: EmptyObject, options?: Options]
: [args: FuncRef["_args"], options?: Options];
> =
AreAllPropertiesOptional<FuncRef['_args']> extends true
? [args?: FuncRef['_args'], options?: Options]
: [args: FuncRef['_args'], options?: Options]

/**
* Given a {@link FunctionReference}, get the return type of the function.
Expand Down
16 changes: 16 additions & 0 deletions src/server/registration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,22 @@ export type ArgsArray = OneArgArray | NoArgsArray;
*/
export type EmptyObject = Record<string, never>;

/**
* Is a property key of a type optional
*/
export type IsOptionalKey<T, K extends keyof T> =
object extends Pick<T, K> ? true : false

/**
* Are all properties of a type optional
*/
export type AreAllPropertiesOptional<T> =
true extends {
[K in keyof T]: IsOptionalKey<T, K> extends true ? never : true
}[keyof T]
? false
: true

/**
* Convert an {@link ArgsArray} into a single object type.
*
Expand Down