Improve Type Inference for an Async Function
In this exercise, we are dealing with an async function named fetchData
that fetches data from a URL and returns a result.
If the fetch operation fails, this function returns a tuple. The first member of this tuple contains the error message and the second member is irrelevant in this case.
If t
Transcript
00:00 In this exercise, we have an async fetch data function. What this does is it returns some sort of result. If the result is not okay, it returns a tuple containing the error in the first member of that tuple. And then otherwise it returns some data and that data is basically, it then returns undefined in the first member of the tuple
00:19 and then returns the data in the second member of the tuple. So you can use it kind of like this where we say await fetch data and it's not going to throw an error here. It's actually just going to basically return the error and the error appears in the end. So it's kind of like a result type. But what we're getting here is that both of these are now being inferred as any.
00:38 That's really weird. And what I would like you to do is try to find a technique by changing the code inside the fetch data function. So basically say, okay, like when we look at this, if we hover over fetch data, it's currently returning a promise with any array. So an array of any.
00:56 We actually want it to infer a tuple here. So there are two approaches. You could either use a return type or you could change some of the annotations within this part of the function. So within the return values of the function. So this return value and this return value and add a little annotation there
01:14 to see if you could get it to infer a tuple of those positions. Good luck.