How can i infer optional query params
I am infering the return type of an nitro endpoint with
InternalApi:
How can i infer an optional query parameter dependend return type for my used route? The return type is different if i send the query param includePelicanSerer, so i have to change my type in this case.3 Replies
Hi @Luex. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
I'm sorry, but the knowledge sources do not contain explicit information about inferring different return types for a Nitro endpoint based on optional query parameters using
InternalApi or a similar type utility. The documentation covers how to define API routes, handle query parameters, and how Nitro generates types for API routes, but it does not describe a built-in way to infer conditional return types based on the presence or absence of specific query parameters in the type system.
You may need to implement your own type logic or use function overloads/typescript conditional types in your own code, but this approach is not documented in the provided Nuxt or Nitro documentation. If you need more advanced type inference based on query parameters, you might need to look for community solutions or open an issue for further guidance from the Nuxt/Nitro team.
For reference, Nitro will generate types for API routes, but these types are not automatically conditional on query parameters—they are based on the handler's return type as defined in your endpoint file, not on runtime query parameter values Server Engine.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@1355686004618428456> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
This is my fetch code:
InternalApi only infers the type without the query param.