Skip to content

Extracting Success Type from Union Type in TypeScript for useState #1641

Answered by RobinTail
yuuri111 asked this question in Q&A
Discussion options

You must be logged in to vote

Hello @yuuri111 ,

The thing you asked for: splitResponse option in the Integration::constructor() argument (added in v16.0.0).

The thing you didn't ask for:

const client = new ExpressZodAPIClient(async (method, path, params) => {
  // ... shortened
  return response.json();
});

export const getSomething = async () => {
  const response = await client.provide("get", "/v1/something", {});
  if (response.status === "error") {
    throw new Error(response.error.message);
  }
  return response.data.thatThing;
};

In this case, getSomething() only returns the positive response and there is no need to Extract it.
So that in your React App you can do

useState<ReturnType<typeof getSomething> | null>(

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by RobinTail
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants