8000 TypeScript type error in `useFetch` default option with `ResT` generic and `transform` returning `unknown` when making external server requests · Issue #22164 · nuxt/nuxt · GitHub
[go: up one dir, main page]

Skip to content
TypeScript type error in useFetch default option with ResT generic and transform returning unknown when making external server requests #22164
@Rock070

Description

@Rock070

Environment

Node Version: v16.20.0
Nuxt Version: 3.6.2

Reproduction

https://stackblitz.com/edit/nuxt-starter-xrqqhx?file=composables%2Ftest.ts

Describe the bug

When using useFetch with the ResT generic, a TypeScript type error occurs with the default option. The error message is as follows:

useFetch<number[]>('/api/count', {
  default: () => [0]
}
Type '() => number[]' is not assignable to type '() => Ref<null> | null'.

However, the error can be resolved by using the following syntax:

useFetch<number[]>('/api/count', {
  default: (): number[] => [0]
}

But when making requests to an external server, using this syntax causes the res parameter in the transform function to be of type unknown instead of number[]. An example of this issue is shown below:

const { data: data2 } = useFetch('https://reqres.in/api/users?page=2', {
  default: (): number[] => [],
  transform(res){
    return res.map(i => i);
  }
});
'res' is of type 'unknown'.

Additional context

No response

Logs

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0