diff --git a/npm-packages/docs/docs/functions/actions.mdx b/npm-packages/docs/docs/functions/actions.mdx index dc9fc8192..1a081d815 100644 --- a/npm-packages/docs/docs/functions/actions.mdx +++ b/npm-packages/docs/docs/functions/actions.mdx @@ -9,7 +9,8 @@ import Context from "!!raw-loader!@site/../private-demos/snippets/convex/actions import ContextRunQuery from "!!raw-loader!@site/../private-demos/snippets/convex/myFunctions.ts"; import ContextRunMutation from "!!raw-loader!@site/../private-demos/snippets/convex/actionsContextRunMutation.ts"; import CircularError from "!!raw-loader!@site/../private-demos/snippets/convex/actionsCircularError.ts"; -import CircularErrorFixed from "!!raw-loader!@site/../private-demos/snippets/convex/actionsCircularErrorFixed.ts"; +import CircularErrorFixedResults from "!!raw-loader!@site/../private-demos/snippets/convex/actionsCircularErrorFixedResults.ts"; +import CircularErrorFixedReturn from "!!raw-loader!@site/../private-demos/snippets/convex/actionsCircularErrorFixedReturn.ts"; import NPM from "!!raw-loader!@site/../private-demos/snippets/convex/actionsNPM.ts"; import Node from "!!raw-loader!@site/../private-demos/snippets/convex/actionsNode.ts"; import Call from "!!raw-loader!@site/../private-demos/snippets/src/actionsCall.tsx"; @@ -109,7 +110,7 @@ do: - To search a vector index, use the `vectorSearch` field. Read on about [Vector Search](/docs/search/vector-search.mdx). -#### Dealing with circular type inference +### Dealing with circular type inference
Working around the TypeScript error: some action implicitly has @@ -127,14 +128,22 @@ infer the return type of the action. This is a minimal example of the issue: snippet="tsError" /> -To work around this, you should store the result of the `ctx.runQuery` or -`ctx.runMutation` call in a variable with a type annotation: - - +To work around this, there are two options: + +1. Type the return value of the handler function explicitly: + +2. Type the the result of the `ctx.runQuery` or `ctx.runMutation` call explicitly: + TypeScript will check that the type annotation matches what the called query or mutation returns, so you don't lose any type safety. diff --git a/npm-packages/private-demos/snippets/convex/_generated/api.d.ts b/npm-packages/private-demos/snippets/convex/_generated/api.d.ts index 652fa5d57..39102aaab 100644 --- a/npm-packages/private-demos/snippets/convex/_generated/api.d.ts +++ b/npm-packages/private-demos/snippets/convex/_generated/api.d.ts @@ -15,7 +15,8 @@ import type { } from "convex/server"; import type * as actionsArgsWithValidation from "../actionsArgsWithValidation.js"; import type * as actionsCircularError from "../actionsCircularError.js"; -import type * as actionsCircularErrorFixed from "../actionsCircularErrorFixed.js"; +import type * as actionsCircularErrorFixedResults from "../actionsCircularErrorFixedResults.js"; +import type * as actionsCircularErrorFixedReturn from "../actionsCircularErrorFixedReturn.js"; import type * as actionsConstructor from "../actionsConstructor.js"; import type * as actionsContext from "../actionsContext.js"; import type * as actionsContextRunMutation from "../actionsContextRunMutation.js"; @@ -110,7 +111,8 @@ import type * as writingDataReplace from "../writingDataReplace.js"; declare const fullApi: ApiFromModules<{ actionsArgsWithValidation: typeof actionsArgsWithValidation; actionsCircularError: typeof actionsCircularError; - actionsCircularErrorFixed: typeof actionsCircularErrorFixed; + actionsCircularErrorFixedResults: typeof actionsCircularErrorFixedResults; + actionsCircularErrorFixedReturn: typeof actionsCircularErrorFixedReturn; actionsConstructor: typeof actionsConstructor; actionsContext: typeof actionsContext; actionsContextRunMutation: typeof actionsContextRunMutation; diff --git a/npm-packages/private-demos/snippets/convex/actionsCircularErrorFixed.ts b/npm-packages/private-demos/snippets/convex/actionsCircularErrorFixedResults.ts similarity index 100% rename from npm-packages/private-demos/snippets/convex/actionsCircularErrorFixed.ts rename to npm-packages/private-demos/snippets/convex/actionsCircularErrorFixedResults.ts diff --git a/npm-packages/private-demos/snippets/convex/actionsCircularErrorFixedReturn.ts b/npm-packages/private-demos/snippets/convex/actionsCircularErrorFixedReturn.ts new file mode 100644 index 000000000..a55982d9a --- /dev/null +++ b/npm-packages/private-demos/snippets/convex/actionsCircularErrorFixedReturn.ts @@ -0,0 +1,12 @@ +import { api } from "./_generated/api"; +import { action } from "./_generated/server"; + +// @snippet start fixed +export const myAction = action({ + args: {}, + handler: async (ctx): Promise => { + const result = await ctx.runQuery(api.myFunctions.getSomething); + return result; + }, +}); +// @snippet end fixed