diff --git a/src/react/client.ts b/src/react/client.ts index 1e0ad97..b6ac388 100644 --- a/src/react/client.ts +++ b/src/react/client.ts @@ -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, @@ -606,9 +606,9 @@ export const ConvexProvider: React.FC<{ }; export type OptionalRestArgsOrSkip> = - FuncRef["_args"] extends EmptyObject - ? [args?: EmptyObject | "skip"] - : [args: FuncRef["_args"] | "skip"]; + AreAllPropertiesOptional extends true + ? [args?: FuncRef['_args'] | "skip"] + : [args: FuncRef['_args'] | "skip"]; /** * Load a reactive query within a React component. diff --git a/src/server/api.ts b/src/server/api.ts index f5c1aef..6951c23 100644 --- a/src/server/api.ts +++ b/src/server/api.ts @@ -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"; @@ -444,9 +445,9 @@ export type FunctionArgs = * @public */ export type OptionalRestArgs = - FuncRef["_args"] extends EmptyObject - ? [args?: EmptyObject] - : [args: FuncRef["_args"]]; + AreAllPropertiesOptional extends true + ? [args?: FuncRef['_args']] + : [args: FuncRef['_args']] /** * A tuple type of the (maybe optional) arguments to `FuncRef`, followed by an options @@ -460,9 +461,10 @@ export type OptionalRestArgs = export type ArgsAndOptions< FuncRef extends AnyFunctionReference, Options, -> = FuncRef["_args"] extends EmptyObject - ? [args?: EmptyObject, options?: Options] - : [args: FuncRef["_args"], options?: Options]; +> = + AreAllPropertiesOptional extends true + ? [args?: FuncRef['_args'], options?: Options] + : [args: FuncRef['_args'], options?: Options] /** * Given a {@link FunctionReference}, get the return type of the function. diff --git a/src/server/registration.ts b/src/server/registration.ts index 4b1c05e..d229b90 100644 --- a/src/server/registration.ts +++ b/src/server/registration.ts @@ -307,6 +307,22 @@ export type ArgsArray = OneArgArray | NoArgsArray; */ export type EmptyObject = Record; +/** + * Is a property key of a type optional + */ +export type IsOptionalKey = + object extends Pick ? true : false + +/** + * Are all properties of a type optional + */ +export type AreAllPropertiesOptional = + true extends { + [K in keyof T]: IsOptionalKey extends true ? never : true + }[keyof T] + ? false + : true + /** * Convert an {@link ArgsArray} into a single object type. *