Optionally Infer the Return Type of a Function

Here’s what the solution looks like:

Let’s talk through it.

First, we want T to extend a function that returns a Promise.

Inside the Promise is an object that will contain the props. Then we infer those props P. Then we return P or never if there aren’t any.

This pattern is useful when working with functions where you want to extract something but you might not have access to its internals or you don’t want to declare a type annotation for.

The type can coming from the function itself!