Languages
[Edit]
EN

TypeScript - get function return type

5 points
Created by:
a_horse
538

In this short article we would like to show how to get type of object returned by function in TypeScript.

Quick solution:

const MyFunction = (): string => { return ''; }
type MyType = ReturnType<typeof MyFunction>; // string

Practical example:

type Action1 = () => void;
type Action2 = () => string;

const Function1 = (): void => {
    console.log('void return');
};
const Function2 = (text: string): string => {
    return `Text: ${text}`;
};

type Action1Result = ReturnType<Action1>;             // void
type Action2Result = ReturnType<Action2>;             // string

type Function1Result = ReturnType<typeof Function1>;  // void
type Function2Result = ReturnType<typeof Function2>;  // string

type Function1Result = ReturnType<() => void>;        // void
type Function2Result = ReturnType<() => string>;      // string

Alternative titles

  1. TypeScript - get function result type
  2. TypeScript - obtain function return type
  3. TypeScript - extract function return type
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

TypeScript

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join