#inferred return type any

3 messages · Page 1 of 1 (latest)

stray thorn
#

I have this query that returns a string

export const test = query({
  args: {},
  handler: async () => {
    return "Hello, world!";
  }
});

but convex dev creates an api.d.ts where this is typed as

test: FunctionReference<"query", "public", {}, any>;

shouldn't it infer its return type automatically and type is as

test: FunctionReference<"query", "public", {}, string>;
red deltaBOT
#

Hi @stray thorn. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

This behavior is likely caused by the static code generation setting being enabled in your project. When staticApi: true is set in convex.json, functions no longer have return type inference and will default to any if they don't have a returns validator. [static code generation]

Check your convex.json for this configuration:

{
  "codegen": {
    "staticApi": true
  }
}

If that's the case, you have two options:

Option 1: Remove staticApi: true from your convex.json to restore dynamic inference.

Option 2: Add a returns validator to your function so the type is explicit even with static codegen:

import { query } from "./_generated/server";
import { v } from "convex/values";

export const test = query({
  args: {},
  returns: v.string(),
  handler: async () => {
    return "Hello, world!";
  }
});

Without static codegen enabled, Convex should infer the return type automatically from the handler. If you're not using staticApi: true and still seeing any, make sure convex dev is running and has successfully completed code generation (look for the green check "Convex functions ready!" message).

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: