beraliv

ReturnType under the hood in TypeScript

Example of ReturnType use
Example of ReturnType use

We start solving medium challenges today. And we are going to discuss Get Return Type

To get rid of repetitive code we sometimes need to reuse return type of any functions. Let's do that ⤵️

Unwrapping values from types

In Unwrapping the Promises we already discussed that Type inference in conditional types is used to get an inferred type variable in the true branch of the conditional type:

Example of conditional type
Example of conditional type

For functions we need to have as general type as possible to cover all types of functions:

Find a general function
Find a general function

This is useful in conditional types to infer the value:

Solution
Solution

If you interested in test cases, please have a look at Playground 💻

typescript

Comments

Alexey Berezin profile image

Written by Alexey Berezin who loves London 🏴󠁧󠁢󠁥󠁮󠁧󠁿, players ⏯ and TypeScript 🦺 Follow me on Twitter