Type Transformations Workshop (4 exercises)
Problem

Assign Types Based on a Function Return Value

In this exercise, we have a function called myFunc which we can see returns a string when hovering over the name:

We want to somehow take the knowledge of what myFunc returns and stick it inside MyFuncReturn.

In this case we need to change this unknown to somehow be a string.

Of course, we could just manually add the type, but then if the return of myFunc changes, then the type may not be the same anymore.

Your challenge is to extract the information about the return type from myFunc into the MyFuncReturn type.

To accomplish this you will need to use the typeof operator, as well as finding a utility type in the TypeScript docs.

Transcript

[0:00] In this exercise, we have a function called myFunc, which we can see returns a string here. If we hover over it, we can see it's returning a string.
[0:09] What we want to do is somehow take that knowledge of what this function returns and stick it inside MyFuncReturn. We need to change this unknown to somehow be a string. Of course, we could just type this out, but then if this changes then it's not going to be the same anymore.

[0:25] What we want to do is extract the information, the ReturnType from myFunc into this type. What you'll need is some TS utility types and probably also the type of operator. Good luck.