DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Java to TypeScript in a Day: AI Migration Playbook
  • When TypeScript Types Meet Untrusted AI: Building Type-Safe LLM Pipelines With Runtime Validation
  • Beyond the Chatbot: Engineering a Real-World GitHub Auditor in TypeScript
  • Resilient API Consumption in Unreliable Enterprise Networks (TypeScript/React)

Trending

  • How to Keep AI Agents From Going Rogue Using Docker Sandbox
  • MQTT, Kafka, NATS, or Zenoh? Choosing Edge-to-Cloud Messaging by Traffic Pattern
  • Top Gen AI Frameworks for Go in 2026: A Hands-on Comparison
  • Designing for Hot Keys: When Distributed Systems Stop Distributing

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. JavaScript
  4. How the TypeScript ReturnType Works

How the TypeScript ReturnType Works

Let's look at how the TypeScript ReturnType Utility Type works

By 
Johnny Simpson
·
May. 16, 22 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
4.7K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

The ReturnType in TypeScript is a utility type that is quite similar to the Parameters Type. It lets you take the return output of a function, and construct a type based on it.

The ReturnType Utility Type

The ReturnType utility type is very useful in situations where the output of a specific function needs to be taken in by another function. In that scenario, you might create a new, custom type, that the output of a function constrains itself to.

Let's look at a silly example to put it into context. Below, we define a new type, which has two properties, a, and b, both of which are numbers. A function then turns all numbers on this object into strings and returns a new type. We define a custom type, called Data, which expects a and b to be strings.

TypeScript
 
function sendData(a: number, b: number) {
    return {
        a: `${a}`,
        b: `${b}`
    }
}

type Data = {
    a: string,
    b: string
}

function consoleData(data:Data) {
    console.log(JSON.stringify(data));
}

let stringifyNumbers = sendData(1, 2);
consoleData(stringifyNumbers);

Since consoleData expects data to be of the format Data, TypeScript throws an error if a or b are numbers. Our sendData function fixes that, by converting a and b to strings.

The issue with this setup is if we added or changed sendData, our input data then Data would need to be updated too. That's not a big deal, but it's an easy source of bugs. As such, we can instead use ReturnType to simplify our type declaration. Our Data type can be written like so:

TypeScript
 
function sendData(a: number, b: number) {
    return {
        a: `${a}`,
        b: `${b}`
    }
}
type Data = ReturnType<typeof sendData>
// The same as writing:
// type Data = {
//     a: string,
//     b: string
// }

Since sendData returns data in type { a: string, b: string }, Data becomes that type. It means we don't have to maintain two copies of the output from sendData - instead, we have one, inside the function, and a type that conforms to that, simplifying our code.

TypeScript

Published at DZone with permission of Johnny Simpson. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Java to TypeScript in a Day: AI Migration Playbook
  • When TypeScript Types Meet Untrusted AI: Building Type-Safe LLM Pipelines With Runtime Validation
  • Beyond the Chatbot: Engineering a Real-World GitHub Auditor in TypeScript
  • Resilient API Consumption in Unreliable Enterprise Networks (TypeScript/React)

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook