Beginner's TypeScript Section 18 exercises

Function Return Type Annotations

Continuing from last time, we have a User that includes an array of posts.

This time instead of having a defaultUser, we instead have a makeUser function that should return a user.

const makeUser = () => {
return {};

At this current starting point, the function is ret

Loading exercise


0:00 We've got a similar problem here to what we had before. We've got our User, which is using Array of Posts, which has the Post here, and we've got a makeUser function. Instead of doing a default user, we're now just returning this makeUser function here.

0:16 Inside here is supposed to represent a user, except we've got a bunch of errors down here. What it's saying is that the function, or the thing that gets returned from makeUser, which is this, is just an empty object.

0:29 If I add some stuff in here and say blah, blah, blah, blah, then it's going to grab that and put it there. This is a pretty good lesson in itself, is that you don't need to say to TypeScript what a function returns.

0:43 In this case, it's going to be pretty nice to ensure that makeUser always returns a user, very similar to the problem we had before, where we wanted to make sure that the error message was on the correct line.

0:54 Here we've got lots of error messages, and they're all pretty confusing and fairly hard to understand. How do we annotate our function to make sure that makeUser always returns a user?