Typings: data depending on error and loading
See original GitHub issueIf 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:
- Created 5 years ago
- Comments:10 (1 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

I think that
datais alwaysTData | {}in Apollo. Would it be best to do this too here?Not taking
partialsanderrorsinto account, i was thinking about something along these lines (using discriminated unions, simplified example):If something like this was possible, it would clearly help! 👍