-
-
Notifications
You must be signed in to change notification settings - Fork 466
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
Comments
With further investigating, this issue was introduced in the 0.9.0next. Which I downloaded for the middleware, will post under that thread |
This seems to exist in |
So I think this may have something to do with how TypeScript’s To be honest I’m not sure how to easily fix this—only allow the explicit |
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 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: |
I confirm I have both problems 🥲 I also tested in both 8 and 9, and the problem is present. |
This issue is stale because it has been open for 90 days with no activity. If there is no activity in the next 7 days, the issue will be closed. |
@gittgott good idea. I wrapped the init parameter in import { FetchResponse, MaybeOptionalInit, Middleware } from "openapi-fetch"
import {
HasRequiredKeys,
HttpMethod,
MediaType,
PathsWithMethod,
} from "openapi-typescript-helpers"
type ClientMethod<
Paths extends Record<string, Record<HttpMethod, {}>>,
Method extends HttpMethod,
Media extends MediaType,
> = <
Path extends PathsWithMethod<Paths, Method>,
Init extends MaybeOptionalInit<Paths[Path], Method>,
>(
url: Path,
...init: NoInfer<
HasRequiredKeys<Init> extends never
? [(Init & { [key: string]: unknown })?] // note: the arbitrary [key: string]: addition MUST happen here after all the inference happens (otherwise TS can’t infer if it’s required or not)
: [Init & { [key: string]: unknown }]
>
) => Promise<FetchResponse<Paths[Path][Method], Init, Media>>
export interface Client<Paths extends {}, Media extends MediaType = MediaType> {
/** Call a GET endpoint */
GET: ClientMethod<Paths, "get", Media>
/** Call a PUT endpoint */
PUT: ClientMethod<Paths, "put", Media>
/** Call a POST endpoint */
POST: ClientMethod<Paths, "post", Media>
/** Call a DELETE endpoint */
DELETE: ClientMethod<Paths, "delete", Media>
/** Call a OPTIONS endpoint */
OPTIONS: ClientMethod<Paths, "options", Media>
/** Call a HEAD endpoint */
HEAD: ClientMethod<Paths, "head", Media>
/** Call a PATCH endpoint */
PATCH: ClientMethod<Paths, "patch", Media>
/** Call a TRACE endpoint */
TRACE: ClientMethod<Paths, "trace", Media>
/** Register middleware */
use(...middleware: Middleware[]): void
/** Unregister middleware */
eject(...middleware: Middleware[]): void
}
export api = createClient<paths>(...) as Client<paths> For backwards compatibility: I wont pretend to understand it, but I saw the following helper type in the tanstack-query codebase, which achieves the same result for me. type NoInfer<T> = [T][T extends any ? 0 : never] |
Thanks all for the detailed info and debugging. This bug is top-of-mind for us, and does need to be fixed. Bugs like this are what’s still PRs are welcome, of course! This is a major bug that does need addressing, as so many of you have pointed out. I personally can’t take this on for the next couple months, but will drop what I’m doing at any time to review PRs that address this. But beyond a simple fix, one thing I’ve realized we’re missing currently is more robust This doesn’t have to happen in the same fix, it could be its own PR, but I think this change in testing would help fixing these a lot less daunting. |
I'm not able to reproduce this in |
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.
First one autocompletes fine.
the second one does not allow for auto complete and does not throw a type error.
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
The text was updated successfully, but these errors were encountered: