Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Property 'statusCode' does not exist on type 'FetchError<any>' #278

Closed
misbahansori opened this issue Aug 24, 2023 · 5 comments
Closed

Property 'statusCode' does not exist on type 'FetchError<any>' #278

misbahansori opened this issue Aug 24, 2023 · 5 comments
Labels
bug Something isn't working

Comments

@misbahansori
Copy link

Environment

Node : v16.14.2
ofetch: "^1.3.2",

Reproduction

try {
    await $fetch("/contact", {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      baseURL: runtimeConfig.public.apiUrl,
      body: form.value,
    });
  } catch (error) {
    if (!(error instanceof FetchError)) throw error;

    if (error.statusCode !== 422) throw error;
              // ^Property 'statusCode' does not exist on type 'FetchError<any>'.ts(2339)
    errors.value = error.data.errors;
                                  // ^Property 'data' does not exist on type 'FetchError<any>'.ts(2339)
  }

Describe the bug

After upgrading to version 1.3.2, the following error message appears. I can confirm that the error message does not appear in version 1.3.1.

Additional context

No response

Logs

No response

@pi0 pi0 added the bug Something isn't working label Aug 24, 2023
@pi0
Copy link
Member

pi0 commented Aug 24, 2023

Thanks for making issue. You can use new IFetchError type to fix. I will try to find a way to fix FetchError (class) types. We had to make this change to avoid unnecessary runtime overhead...

@sharifzadesina
Copy link

It was actually working before.
Now, even I can't access response property. Actually non of IFetchError properties.

@johannschopplich
Copy link
Contributor

@misbahansori @sharifzadesina I had the same problem and provided a PR to ensure TypeScript understands and provides properties like statusCode, statusMessage etc.: #279

For my own purposes before the PR gets merged (or a better solution found 😉), you can re-export the type:

export type { IFetchError as FetchError } from 'ofetch'

Which I did in my Nuxt API Party plugin: johannschopplich/nuxt-api-party@181a5a0

@misbahansori
Copy link
Author

In the meantime, does anyone have a good approach to narrow the types within the catch block? Since the error type is unknown we need to narrow it down. currently, I'm using error instanceof FetchError which has a runtime implication.

@pi0
Copy link
Member

pi0 commented Aug 25, 2023

Should be resolved by #279 in v1.3.3

@pi0 pi0 closed this as completed Aug 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants