Skip to content
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

Add second workaround to circular type inference #51

Closed
wants to merge 1 commit into from
Closed
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
29 changes: 19 additions & 10 deletions npm-packages/docs/docs/functions/actions.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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

<Details summary={<>
Working around the TypeScript error: some action <code>implicitly has
Expand All @@ -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:

<Snippet
title="convex/myFunctions.ts"
source={CircularErrorFixed}
snippet="fixed"
/>
To work around this, there are two options:

1. Type the return value of the handler function explicitly:
<Snippet
title="convex/myFunctions.ts"
source={CircularErrorFixedReturn}
snippet="fixed"
highlightPatterns={["null"]}
/>
2. Type the the result of the `ctx.runQuery` or `ctx.runMutation` call explicitly:
<Snippet
title="convex/myFunctions.ts"
source={CircularErrorFixedResults}
snippet="fixed"
highlightPatterns={["null"]}
/>

TypeScript will check that the type annotation matches what the called query or
mutation returns, so you don't lose any type safety.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<null> => {
const result = await ctx.runQuery(api.myFunctions.getSomething);
return result;
},
});
// @snippet end fixed