Objects 16 exercises
Problem

Working with Partial Data from a Type

In this exercise, we're working with a fetchUser function. This function makes use of the fetch method to access an endpoint named APIUser, and it returns a Promise<User>:


interface User {
id: string;
name: string;
email: string;
role: string;
}
const fetchUser = asy

Loading exercise

Transcript

00:00 In this exercise, we have a FetchUser function. That FetchUser function is calling Fetch and going grabbing something from an endpoint called APIUser. And it's returning a type of PromiseUser. That's correct to use Promise because we're inside an async function. But this user type is not actually correct.

00:18 Imagine that we're getting this interface user from like a shared group of types that represent our database, represent our data structure. We actually, in this function, we're only expecting to receive name and email. It happens to be like, you know, grab something like from an anonymous source, let's say,

00:35 or we're only getting a certain part of that user. Now we want to represent this without changing our initial interface too much. We still want user to represent IDString, NameString, EmailString, and RollString. But we want to say this FetchUser function

00:53 only grabs back NameString and EmailString. And of course we could just like put NameString and EmailString in there, but ideally we want them to keep up to date with the user if we can. So yeah, we just want a single source of truth for the type instead of having like multiple different names and emails scattered all over our code base.

01:13 So your job here is to try to figure out a way to either change this interface inside the Promise type there or change the interface user to figure it out. So I'll just say there are two different ways to fix this or ways to find the solution. The first one is using stuff we've done before with Interface Extends.

01:31 And the other one is with a new type helper called Pick. Good luck.