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.

`useFetch` type return

See original GitHub issue

Environment

  • Operating System: Windows_NT
  • Node Version: v16.13.1
  • Nuxt Version: 3.0.0-27415326.3c563fa
  • Package Manager: yarn@1.22.17
  • Bundler: Vite
  • User Config: -
  • Runtime Modules: -
  • Build Modules: -

I use Visual Studio Code with Volar

Reproduction

In app.vue, I fetch data with useFetch()

<script setup lang="ts">
interface Book {
  title: string
  authors: string[]
  serie?: string
}

const { data } = await useFetch<Book>('/books?per-page=32', {
  baseURL: 'https://bookshelves.ink/api',
  lazy: true,
})
console.log(data.value)
</script>

CodeSandbox is available.

Describe the bug

When I want to add type to useFetch(), I’ve an error

Type ‘Book’ does not satisfy the constraint ‘string’.

<script setup lang="ts">
const { data } = await useFetch<Book>('/books?per-page=32', {
  baseURL: 'https://bookshelves.ink/api',
  lazy: true,
})
</script>

It works with useAsyncData()

Additional context

image

Logs

No response

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
danielroecommented, Feb 16, 2022

let’s leave it open as a documentation issue

1reaction
danielroecommented, Feb 16, 2022

You can update it like this:

  script setup lang="ts">
- const { data } = await useFetch<Book>('/books?per-page=32', {
+ const { data } = await useFetch<string, Book>('/books?per-page=32', {
    baseURL: 'https://bookshelves.ink/api',
    lazy: true,
  })
</script>

(The first generic is the type of the URL - it can either be a string or a typed string associated with a particular return type …)

Would be nice to document this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to specify useFetch data return type in Nuxt
I am using typescript and I wish to define the type of data that I will get. How do I specify this? <script...
Read more >
useFetch
React hook for making isomorphic http requests.
Read more >
useFetch() react hook - usehooks-ts
Here is a React Hook which aims to retrieve data on an API using the native Fetch API. I used a reducer to...
Read more >
useFetch - VueUse
Setting the request method and return type #. The request method and return type can be set by adding the appropriate methods to...
Read more >
useFetch - Raycast API
useFetch. Hook which fetches the URL and returns the AsyncState ... By default, the hook will return response.json() if the response has a...
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