ReturnType
Explore how to use TypeScript's ReturnType mapped type to extract and work with the return types of functions. Understand handling of multiple return types, asynchronous function typing, and how this feature enhances code safety and type accuracy.
We'll cover the following...
Extracting the return type of a function
In some cases, you may want to extract the returned type of a function. TypeScript comes with a ReturnType mapping function that gives you this information.
For example, if you have a function that returns a string, you can use ReturnType<yourFunction> and it will return the type string. The following code on line 5 assigns to a variable, the type which is a string because the return type of the function getName is a string.
ReturnType becomes important in case the return type changes in the future. Then, the ...