How To Use the Built-In ReturnType Type in TypeScript

A deep dive into TypeScript’s ReturnType utility type, where we’re also going to use Awaited 🧟

Dr. Derek Austin 🥳
Totally TypeScript

--

Yes, dogs love ReturnType! They’re masters of the built-in Node Fetch API! Photo by Dena Simard on Unsplash

The Lowdown on ReturnType in TypeScript

TypeScript, as we all know, is like JavaScript that’s had too much coffee: hyper aware and super specific. One of those caffeinated features is the built-in utility type ReturnType. I actually find myself using this one a lot!

At its core, ReturnType retrieves the return type of a function. This is particularly useful when we're trying to avoid redundant type definitions.

My TypeScript coding workflow involves spending large amounts of time hovering my mouse over functions to look at their inferred return types.

I copy-paste various pieces of the VS Code Intellisense inferred type, frequently! But what if there were a better way… That’s ReturnType!

Quick Example of Using ReturnType & Promises

Let’s imagine that we’re going to look up a list of mapaches named Rocky, Rico, and Rita. We’ll do so asynchronously. What’s the ReturnType?

/** Our old friend, the fetcher that resolves promises. */
function fetchMapaches()…

--

--

Dr. Derek Austin 🥳
Totally TypeScript

I write about real-world programming career advice, MongoDB vs. PostgreSQL, Git, React, JavaScript, VS Code, TypeScript, and Next.js. Doctor of Physical Therapy