Replies: 1 comment 1 reply
-
This seems related to #1440, a bit. The openapi-typescript-helpers package is meant to help a bit with creating some of these layered inferences yourself, but it’s really hard to do in general, and it usually takes a bit of time to get what you want. As a general tip: a lot of good inference comes from making full use of the type ResponseData = response extends { data: any } ? response["data"] : never;
const data: ResponseData = response.data; (this is contrived, and doesn’t do what you want, but it just shows how I debug these things step-by-step) The second tip is when you’re crossing the boundary into Really, a lot of this advanced TypeScript stuff is just taking it slow, and taking it step-by-step. None of the type inference in this library were created first pass; they were built step-by-step and refined and cleaned up over a long time. Just try and introspect the next layer, and keep going layer-by-layer until you get where you want. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm using the
openapi-typescript
andopenapi-fetch
library on top of my NextJS project with server actions to handle form data. One reference implementation looks pretty much like this:Basically it takes the data from the client side, validates it, sends the request and performs basic validation. The API returns a header containing a unique request ID, so I'm returning this as well. Much of that code is pretty boilerplate, ideally I'd want to do something like this:
Where the
handleResponse
function would do all the validation. I'm having quite some trouble to come up with the method signature though, as the type ofFetchResponse
is generic:The data attribute uses types which are not exported by the library. Any idea how to handle this use-case without repeating myself?
Beta Was this translation helpful? Give feedback.
All reactions