Type error: implicitly has type 'any' because it does not have a type annotation
Hello, I have a type error but can't figure it out why its causing it.
When I return the
order
, the variable order
, createInvoice
and handler
turns red with a type error. (image attached).
What am I doing wrong?28 Replies
Thanks for posting in <#1088161997662724167>.
Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets.
- Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.)
- Use search.convex.dev to search Docs, Stack, and Discord all at once.
- Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI.
- Avoid tagging staff unless specifically instructed.
Thank you!
Actions | Convex Developer Hub
Actions can call third party services to do things such as processing a payment
This is the
internal.orders.getOrderByIdInternal
internalQuery function.
Hey @lee, thanks for the resource. Why the type is not inferred? The return type of my función is big. Do I need create a TS type or interface mapping the returned object? Or what recommended pattern is best for this case?i would search "circular type" in discord. this is how i understand it https://discord.com/channels/1019350475847499849/1236968484512989234/1237339765418758195
i don't know if there's a better way than annotating the return type. your type does look complex, that's a bummer. maybe @ballingt has a solution?
@lee @ballingt I create a "function return type" from my internal function, and the type is working, but the error still exists.
This is how I'm using it, but if I return the order, I got an error in the function variable and in handler property.
Is the error about the output not matching the
output: v...
validator? Or somethin gelse?I'm trying to returning a value in an action that depends on the result of calling ctx.runQuery or ctx.runMutation.
Actions | Convex Developer Hub
Actions can call third party services to do things such as processing a payment
As Lee point out this link
When you say the error still exists, could you show a screenshot of the error? There are a few ways it could manifest. (re "got an error in the function variable and in handler property.")
Big picture we need to make this more convenient, and some changes to the way we declare functions may help.
But it's going to be a while for big changes like that, until then we need clear workarounds. The general category of declaring types is usually enough, but that's vague, there are several places types could be declared
The type you're providing for
order
is superfluous, it's the return type of the query you're running from inside the mutation so it's still circular inference.
You'll have to actually type out the object in this case without referencing the query that's creating the object. I've found in many cases like this that I don't actually need the whole object I'm returning, and only need a few keys. If that happens to be the case the return value will be simpler to type. But either way, you have to provide an independent type for any part of that query reponse that goes into your action response.Hey @ballingt I attached a screen shot of the error at the beginning of this post
In this case I do need the whole object I'm returning. I tried to type out the object, not with all properties, but with some and the error still there
@erquhart
Are there any other type errors in your Convex code?
Especially inference errors like this one
No, those are the only ones @erquhart
This shouldn't work, but try using a separate variable and typing that, eg.:
I am seeing that simply typing it is not enough, it gets tricky when the query return value is complex.
Or, to rephrase, typing is enough, but how to do that correctly gets tricky, and the compiler is often unable to help due to the inference issue.
Thank you @erquhart
Can that getOrderByIdInternal query return null for any reason? Playing around with it and I think that specific case can cause problems
Oh, better yet, can it return two different types?
Yeah it looks like a union return type on the query would still break things, even with accurate typing.
Yes it breaks
It does not like returning the result of any runMutation, runQuery or runAction. Or better said, to depend on the returning result of those.
Minimal repro:
it does give you a type error
Yep, I don't know how to work around it in the repro I just gave. Still trying though.
👍
I think it is common to want to return something that depends on the result of runMutation/Query/Action
Ah, typing the return for the whole action, forgot about that:
That ^^ has no errors
So typing also the returned value
type*
If you type the return value of the action, you don't need to type the return value of
runQuery
That worked for me too!