question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Typings: data depending on error and loading

See original GitHub issue

If there is no error and loading is false, is it even possible to have undefined data?

The example given in the Readme is not possible in typescript, because data might be undefined in the final return statement so we can’t map over data.dogs.

It would be nice if the typings could reflect this.

const Dogs = () => {
  const { data, error, loading } = useQuery(GET_DOGS);
  if (loading) {
    return <div>Loading...</div>;
  };
  if (error) {
    return <div>Error! {error.message}</div>;
  };

  return (
    <ul>
      {data.dogs.map(dog => (
        <li key={dog.id}>{dog.breed}</li>
      ))}
    </ul>
  );
};

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
tleunencommented, Mar 11, 2019

I think that data is always TData | {} in Apollo. Would it be best to do this too here?

1reaction
codepunktcommented, Mar 7, 2019

Not taking partials and errors into account, i was thinking about something along these lines (using discriminated unions, simplified example):

interface QueryHookStateLoading {
  data: undefined
  loading: true
  error: undefined
  networkStatus: NetworkStatus | undefined
}

interface QueryHookStateFailure {
  data: undefined
  loading: false
  error: ApolloError
  networkStatus: NetworkStatus | undefined
}

interface QueryHookStateSuccess<T> {
  data: T
  loading: false
  error: undefined
  networkStatus: NetworkStatus | undefined
}

type QueryHookState<T> =
  | QueryHookStateLoading
  | QueryHookStateFailure
  | QueryHookStateSuccess<T>

If something like this was possible, it would clearly help! 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

rest call should return data as T if not loading and no errors
when using fetch to post data in a react hook is it possible to infer that if loading is done, and there are...
Read more >
Improving TypeScript error handling with exhaustive type ...
In this article, we'll cover a few of the most common approaches for error handling in TypeScript (TS). We'll talk about most common...
Read more >
Expressive error handling in TypeScript and benefits for ...
Correctly handling and throwing errors in Javascript, or Typescript in our case, is always a painful experience and a journey with many places ......
Read more >
Type-Safe Error Handling In TypeScript
The compiler is no longer able to tell you whether your code is safe from runtime errors. In other words, using throw is...
Read more >
TypeScript with Apollo Client
// our query's result, data, is typed! 19. const { loading, data } ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found