Challenges 7 exercises

Improve a Fetch Function to Handle Missing Type Arguments

Here is an async function fetchData that takes in a type argument of TResult and a real argument of url: string, and then returns a Promise<TResult>:

const fetchData = async <TResult>(url: string): Promise<TResult> => {
const data = await fetch(url).then((response) => respon

Loading exercise


0:00 This is a fun little exercise. We have a fetchData function, which is an async function which takes in a type argument of T results, a real argument of URL string, and then returns promise T results. Very, very similar to any fetch wrapper that you've seen before.

0:16 FetchData here, basically, you're passing name string, and you get back name string after awaiting it. Then = "Luke Skywalker." But we want to make this API pretty bulletproof because currently, this data is coming back as unknown, which is fine, but we would love to have a error message here saying how to get it not unknown.

0:41 Because one thing you could do is just pass a name string here, for instance, and this would be fine, but it would be great if we could have a more idiomatic way to do it and have a way that was consistent across the whole codebase.

0:55 I would like you to find a way where if the user doesn't pass a type argument to fetchData, I want type of data to equal "you must pass a type argument to fetchData." Nasty one. This is an interesting little challenge. You won't need to do anything too fancy here. The solution is simpler than you expect. I'll leave that with you. Good luck.