Essential Types And Notations 20 exercises
Problem

Function Types

Here we have a modifyUser function that takes in an array of users, an id of the user that we want to change, and a makeChange function that makes that change:


type User = {
id: string;
name: string;
};
const modifyUser = (user: User[], id: string, makeChange) => {
// red

Loading exercise

Transcript

00:00 In this exercise, we're going to look at a modify user function. Now, this is a similar challenge to what you may have seen in the past where you want to map over an array of something and then change something in that array with a certain ID. So here we're taking an array of users, then an ID of the user that we want to change,

00:18 and then a function which makes that change. So the idea is that we map over each user. If the user ID corresponds to the ID that we want, then we make the change. So the idea is inside makeChange is you would do some sort of mutation with that user. So let's take a look down here.

00:34 We basically have modifyUser where we have users, the array. We're passing in one, so this is John here. And we're changing the user from this user here and giving it a name of Vacas instead. And now this modifyUser here, we shouldn't be allowed to return something

00:52 which doesn't correspond to the right name there or doesn't correspond to the right type. So here we're returning name 1, 2, 3. That doesn't correspond with the user name up here. So your job is to work out how to make a type that is a function because this is what we want.

01:09 We want to represent makeChange as a type which is a function. And that function is going to take in a user and return a user. So that's your job is to try and figure out what type annotation you can give makeChange for these errors to go away.