Replies: 8 comments 2 replies
-
You don't have to do that. async function getSomethingFromAPI() {
const response = await client.provide(/*...*/);
if (response.status === "error") {
throw new Error(response.error.message);
}
return response.data;
} This way Typescript will automatically narrow down the return type of your function to the positive response only, while the case of the negative one is also handled, @shawncarr |
Beta Was this translation helpful? Give feedback.
-
Perhaps I don't understand your use case. Please provide more reasoning if the code sample above does not solve your problem.
Those types are actually stored separately and combined intentionally into one, when generating the client. |
Beta Was this translation helpful? Give feedback.
-
Check out this sample frontend API consumer, with a variety of usage examples, @shawncarr : https://github.com/RobinTail/incaseofmydeath/blob/master/frontend/src/api.ts |
Beta Was this translation helpful? Give feedback.
-
Your example does work in a project does not require explicit return types. But if you need explicit return types then you end up with something like
Which I usually do a type to shorten it up and have re-usability
|
Beta Was this translation helpful? Give feedback.
-
Check this out, @shawncarr : |
Beta Was this translation helpful? Give feedback.
-
@shawncarr , 🚀 I've just published dev preview version |
Beta Was this translation helpful? Give feedback.
-
I believe it is working as expected. |
Beta Was this translation helpful? Give feedback.
-
Right now you provide a
Response
interface that looks something likeBut to get the typing for the Positive Status of that response I have to do something like
Then repeat for the Negative status for each response. For each client that it would be imported into.
IMHO it would be nice if you generate and return a
Responses
type something likeI know it wouldn't be that simple as this would only work for the
defaultResultHandler
as the status might not be there in a different result handler.Thoughts?
Beta Was this translation helpful? Give feedback.
All reactions