-
-
Notifications
You must be signed in to change notification settings - Fork 488
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
[feat]: parseAs options reports correct data type #1428
Conversation
|
@drwpow I've made a draft PR which only changes GET requests - seem to work fine. Could you check if it looks fine to you? If yes, I'll update to include over http verbs too. In general I think this change makes API more flexible in the future since any option property can be used now to modify return type. |
|
@drwpow ok, I'm a bit stumped at the moment - this pattern doesn't seem to work for some requests which require body/params. I've been able to pinpoint the root of the issue somewhat but have no idea what's causing it - seems like literal types cause issues (like And because literal types cause these issues function call to POST/DELETE etc falls back to use default generic parameter value(which is the value from extends clause) so you get no return type inference. The thing is I don't see anywhere in the code anything which checks for values to be strings or numbers so I don't really have an idea on where to look. I don't even see anything which checks for value beyond 'content' prop so why would it matter which values does it have inside of it. |
Yeah this is a tricky problem for sure. Will dig in later this week; this will take some investigation |
@drwpow in case that would help - when I made a wrapper for our project for making api calls I also had an issue with POST & PATCH verbs where it would say that my provided fetchOptions doesn't extend type type Options =
// Post requests
| FetchOptions<{ requestBody?: { content?: { [x: `${string}/${string}`]: unknown } } }>
// Patch requests
| FetchOptions<{
requestBody?: { content?: { [x: `${string}/${string}`]: unknown } };
parameters: unknown;
}>
// Get requests & the rest
// eslint-disable-next-line @typescript-eslint/no-explicit-any
| FetchOptions<any>; |
This is mostly unrelated, but problems like this are why in openapi-typescript@7 there are a lot more Not saying that 7.x will solve this problem for us or anything—it’s the same problem here either way—but just saying I’ll probably first try and solve this in the 7.x schema types since it theoretically should be simpler, then backport it to 6.x types. |
@drwpow sure, if you think it will be much easier to work with new types I can wait till these changes are made and make my PR around them. |
OK finally got some time to take a crack at it in #1442. In general it’s hard to explain what I did, and there’s also some arbitrary cleanup I did that just made it easier for me to debug, but didn’t actually contribute to the solution. But the short version is your PR laid the correct groundwork but the inference was getting lost from the Instead, we want I’m not saying that there’s not a way to make I think we’ll have to test it a little more, but it’s close! |
Anyway, feel free to cherrypick #1442 and/or pull that into your PR, and test it some more on your end. |
Ok, so the current iteration passes my mini test-suite I've set up with a more complicated openapi schema - I'll give it a day or two to try to make it work with an Options object as I was trying before. |
This is pure insanity but I think I got it solved at least, haha leproblem.mp4 |
wait—why is |
Yup, those two fields are enums - seems like TS inference isn't working properly here and it thinks A friend of mine showed me this and I think it's a related issue. Screen.Recording.2023-11-28.at.16.58.12.mov |
Sorry—was there anything else you wanted to get in before merging? I think this looks great as-is, and happy to merge and kick the tires some more. But wanted to double-check just in case. |
@drwpow Hi, yup, I'm done |
Changes
Closes #1370
How to Review
Improves typing of fetch function by introducing a fetchOptions generic which can then be used to report correct return type.
Checklist
docs/
updated (if necessary)pnpm run update:examples
run (only applicable for openapi-typescript)