Deriving Types From Values 15 exercises
solution

Using the ReturnType Type Helper

Following the pattern we used in the previous exercise, we can use the ReturnType type helper to extract the return type of a function.

Update the User type to use ReturnType with typeof createUser passed in:


type User = ReturnType<typeof createUser>;

Now, User will h

Loading solution

Transcript

00:00 Okay, going by the pattern that we've had before, this should be pretty simple. We can use return type available globally, passing in type of create user. Whoops, create user. There we go. And now we're gonna end up with ID, name, and email all in here. Beautiful stuff. Return type again, it accepts a function,

00:19 so you can't pass in any object into here. And it just means that this complicated type helper up here just returns the return type of the function, no matter what it is. So now if I add something up here, like phone is whatever, then we get an extra attribute inside phone there too. So this is another way that you can derive types from

00:39 values, even if those values are relatively complex. Very cool.