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

fix(nuxt): infer useFetch method when generic is passed #20797

Merged
merged 8 commits into from May 17, 2023

Conversation

Gypsophyllite
Copy link
Contributor

@Gypsophyllite Gypsophyllite commented May 12, 2023

πŸ”— Linked issue

resolves #19077

❓ Type of change

  • πŸ“– Documentation (updates to the documentation, readme or JSdoc annotations)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

I provided a workaround before, but this issue doesn't get solved.

I find that just change the method type 'get' extends AvailableRouterMethod<ReqT> ? 'get' : AvailableRouterMethod<ReqT> into AvailableRouterMethod<ReqT>, and all works fine as I see

πŸ“ Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

Copy link
Member

@danielroe danielroe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you be able to add a test case in test/fixtures/basic/types.ts that was failing before this PR?

@Gypsophyllite
Copy link
Contributor Author

@danielroe I added one, btw, setting the return type and request method explicitly will easily reproduce.

@danielroe
Copy link
Member

Thanks! I've gone ahead and pushed a failing test to show why the type is written as is.

@@ -31,7 +31,7 @@ export function useFetch<
ResT = void,
ErrorT = FetchError,
ReqT extends NitroFetchRequest = NitroFetchRequest,
Method extends AvailableRouterMethod<ReqT> = 'get' extends AvailableRouterMethod<ReqT> ? 'get' : AvailableRouterMethod<ReqT>,
Method extends AvailableRouterMethod<ReqT> = AvailableRouterMethod<ReqT>,
Copy link
Member

@danielroe danielroe May 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about this instead?

Suggested change
Method extends AvailableRouterMethod<ReqT> = AvailableRouterMethod<ReqT>,
Method extends AvailableRouterMethod<ReqT> = ResT extends void ? 'get' extends AvailableRouterMethod<ReqT> ? 'get' : AvailableRouterMethod<ReqT> : AvailableRouterMethod<ReqT>,

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! You are right. I have updated it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also find the reason of broken type when passing uppercase method, maybe its a nitro type error in fetch.ts.
Another PR in nitro

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work - thanks! I think we can go ahead and merge this PR (as it fixes one issue without introducing any regressions) and then update test once nitro PR is merged..

@danielroe danielroe changed the title fix(nuxt): useFetch method type infer error fix(nuxt): infer useFetch method when generic is passed May 17, 2023
@danielroe danielroe merged commit f39eb6e into nuxt:main May 17, 2023
19 checks passed
This was referenced May 17, 2023
eensander added a commit to MaastrichtU-BISS/lawnotation that referenced this pull request Jun 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PUT, PATCH, and DELETE requests
2 participants