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()`TS error when using `useRequestHeader()`

See original GitHub issue

Environment


  • Operating System: Darwin
  • Node Version: v16.16.0
  • Nuxt Version: 3.0.0-rc.10
  • Nitro Version: 0.5.3
  • Package Manager: yarn@1.22.19
  • Builder: vite
  • User Config: -
  • Runtime Modules: -
  • Build Modules: -

Reproduction

I tried to reproduce in stackblitz but didnt get any TS errors so not sure if its an environment thing. I did spin up a completely clean app with nuxi init and the error was there.

<template>
  <div>
    {{ items }}
  </div>
</template>

<script setup lang="ts">
useHead({
  title: "Items",
});

const { data: items } = await useFetch("/api/items", {
  headers: useRequestHeaders(["cookie"]),
});
</script>

Describe the bug

TS error on the header field of userFetch()

Argument of type '{ headers: Record<"cookie", string>; }' is not assignable to parameter of type 'UseFetchOptions<unknown, (res: unknown) => unknown, KeyOfRes<(res: unknown) => unknown>>'.
  Object literal may only specify known properties, but 'headers' does not exist in type 'UseFetchOptions<unknown, (res: unknown) => unknown, KeyOfRes<(res: unknown) => unknown>>'. Did you mean to write 'aheaders'?ts(2345)

Additional context

No response

Logs

No response

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:11 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
GerryWilkocommented, Nov 23, 2022

This appears to be an issue related to this change https://github.com/nuxt/framework/pull/5748

The useRequestHeaders composable was changed to allow for headers not being found and returning undefined

All the fetch composables have the type definition of headers set to Record<string, string> so the current return type of Record<string, string | undefined> from useRequestHeaders is incompatible.

A simple workaround for now is to as Record<string, string> the result of useRequestHeaders.

1reaction
then3rdmancommented, Sep 16, 2022

It is being caused by some external factor. Not sure what yet, or even how… but I will need to add in things bit by bit to see what is causing the issue

Read more comments on GitHub >

github_iconTop Results From Across the Web

useFetch.ts hook react test mock - Stack Overflow
It says, displays (in the test screen.debug()) the "Error" if statement, and even when I left out form the component the if error...
Read more >
Data Fetching · Get Started with Nuxt
The key to making this work is to call the refresh() method returned from the useFetch() composable when a query parameter has changed....
Read more >
useFetch() react hook - usehooks-ts
useFetch(). Here is a React Hook which aims to retrieve data on an API using the native Fetch API. I used a reducer...
Read more >
useFetch
You can do that too var { request, response, // Do not destructure the `response` object! loading, error, data, cache, // .has(), .clear(),...
Read more >
Error Handling - 《Nuxt 3 beta Documentation》 - 书栈网
Example with global error reporting framework ... or (on server side) directly within middleware, plugins or setup() functions.
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