question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Type inference on useLazyFetch with formatted string not working

See original GitHub issue

Environment


  • 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: image

The type gets inferred correctly when not used a formatted string: image

However when I use the formatted string the type inference does not work anymore: image

When hovering over the useLazyFetch method the type contains the given route and when used with the formatted string this is /api/rpa/${string}: image

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:open
  • Created a year ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
danielroecommented, Oct 17, 2022

@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.

1reaction
GionRubitschungcommented, Oct 17, 2022

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: image

  • The put call: image

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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Typescript inference not working correctly - Stack Overflow
The main thing being to type your createModel function. Before you were relying on inference there and you need to define that the...
Read more >
Type inference - Ballerina language
Type inference. Type inference is local and restricted to a single expression. Overuse of type inference can make the code harder to understand....
Read more >
ptype: probabilistic type inference - PURE
In our experiments, we consider five common column types, including Boolean, date, float, integer and string. Table 1 presents the distribution ...
Read more >
@sprintf with a format string - Google Groups
In other words: sure, formatting numeric output is fundamental, but it's a problem with a large solution space, and we don't want to...
Read more >
2795-format-args-implicit-identifiers - The Rust RFC Book
No implicit named argument capture will be performed if the format string is generated ... get_person() is a function call expression, not an...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found