Type inference on useLazyFetch with formatted string not working
See original GitHub issueEnvironment
- Operating System:
Linux
- Node Version:
v16.17.1
- Nuxt Version:
3.0.0-rc.11
- Nitro Version:
0.5.4
- Package Manager:
pnpm@7.12.2
- Builder:
vite
- User Config:
-
- Runtime Modules:
-
- Build Modules:
-
Reproduction
I tried to recreate a codesandbox but it doesn’t really work. The route and response type is build the same way as in my project.
But since the issue is quite simple you just need a nuxt 3 project with typescript and a server route wit a parameter that returns something with a type.
Describe the bug
I am using nuxt 3 with typescript and the composition api. I have noticed something with the type inference on the fetch composables. I don’t receive the the inferred type when using formatted strings for parameters.
When typing in the route into useLazyFetch
I get the correct recommendations for the existing routes:
The type gets inferred correctly when not used a formatted string:
However when I use the formatted string the type inference does not work anymore:
When hovering over the useLazyFetch method the type contains the given route and when used with the formatted string this is /api/rpa/${string}
:
I can define the response type manaully with useLazyFetch<T>
or in my example useLazyFetch<ApiResponse<rpa>>(`/api/rpa/${route.params.id}`)
I haven’t found an explanation in the docs, is there a way to achieve type inference wihtout declaring the type manually?
Additional context
No response
Logs
No response
Issue Analytics
- State:
- Created a year ago
- Comments:7 (4 by maintainers)
Top GitHub Comments
@didavid61202 and I are already talking about this very thing - but at the moment it’s a feature we don’t yet have, so the combined type inference is correct.
Very nice, the type inference works now (don’t know why it works now since I changed nothing in my configuration). There is one more thing that doesn’t work. If you have multiple calls in the same scope nuxt does not know what type to pick. I opened a pr https://github.com/didavid61202/nuxt-starter-62ds6v/pull/1 on your repo where you can see for yourself. I added a put call (update user) that returns a message. Nuxt cannot decide which type to pick even if you set the http method in the options
The get call:
The put call:
As you can see the return type is the same. @danielroe It would be great if nuxt can infer the type also in this scenario, would this be possible?