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

Query param type safety only works for the first param. #1525

Open
1 task
Noahkoole opened this issue Jan 30, 2024 · 5 comments
Open
1 task

Query param type safety only works for the first param. #1525

Noahkoole opened this issue Jan 30, 2024 · 5 comments
Labels
bug Something isn't working good first issue Straightforward problem, solvable for first-time contributors without deep knowledge of the project openapi-fetch Relevant to the openapi-fetch library PRs welcome PRs are welcome to solve this issue!

Comments

@Noahkoole
Copy link

Noahkoole commented Jan 30, 2024

Description

When supplying multiple query params. it seems that when entering the first one correct it turns in to some sort of "Any" type. Allowing for any type of param and no autocomplete either.

image

First one autocompletes fine.

image

the second one does not allow for auto complete and does not throw a type error.

image

this is the object that it expects

Reproduction

Have an endpoint that expects multiple query params and try to fill in more then 1 in the options payload.

Expected result

Type safety when typing multiple

Checklist

@Noahkoole Noahkoole added bug Something isn't working openapi-fetch Relevant to the openapi-fetch library labels Jan 30, 2024
@Noahkoole
Copy link
Author

With further investigating, this issue was introduced in the 0.9.0next. Which I downloaded for the middleware, will post under that thread

@Noahkoole Noahkoole mentioned this issue Jan 30, 2024
3 tasks
@drwpow
Copy link
Contributor

drwpow commented Feb 14, 2024

This seems to exist in 0.8.x as well. Will investigate a fix

@drwpow drwpow reopened this Feb 14, 2024
@drwpow
Copy link
Contributor

drwpow commented Feb 14, 2024

So I think this may have something to do with how TypeScript’s extends keyword works internally—it was originally designed for class inheritance, and thus it doesn’t prevent adding additional properties to the base, so long as the required keys are provided. So in your example, it will allow loffeeeqfqnk because it’s met its base requirements.

To be honest I’m not sure how to easily fix this—only allow the explicit parameters in the OpenAPI schema, and no more. I didn’t dig up any hints in the official TS docs on conditional types, but perhaps some TS wizards reading this would be so kind as to investigate a fix? 🙏

@drwpow drwpow added PRs welcome PRs are welcome to solve this issue! good first issue Straightforward problem, solvable for first-time contributors without deep knowledge of the project labels Feb 14, 2024
@gittgott
Copy link

gittgott commented Feb 19, 2024

Hey @drwpow,

So it seems like there's two issues here.

One is that the params can have arbitrary keys outside of the specified ones. I found that TypeScript 5.4 will be adding a new intrinsic type called NoInfer that might help with this. I put together a small example in the TypeScript playground to demonstrate this. Not 100% sure if it will be as easy as just wrapping the init parameter's type from ClientMethod in NoInfer, but that might work once TypeScript 5.4 is available and able to be used as a minimum version for this package.

The other issue is that once you add one optional parameter to the request, the rest of the optional parameters do not show up in autocomplete. This issue seems specific to version ^0.9.0 of openapi-fetch. I can look into that and make a PR if I find anything.

Edit:
Here's some screenshot comparisons of 0.9.x and 0.8.x. This is using the cat facts api that is in the react-query example in this repo.

Example with 0.9.2:
image

Example with 0.8.2:
image

@clementAC
Copy link

clementAC commented Apr 11, 2024

I confirm I have both problems 🥲 I also tested in both 8 and 9, and the problem is present.
It defeats a little the principle of implementing typescript.
I can try the noInfer but I don't know how

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Straightforward problem, solvable for first-time contributors without deep knowledge of the project openapi-fetch Relevant to the openapi-fetch library PRs welcome PRs are welcome to solve this issue!
Projects
None yet
Development

No branches or pull requests

4 participants