🏡 Home

Typescript Note: ReturnType

I recently need to define the return type of function. But the return type I assigned doesn't seem to work. After looking into the type definition I realize that the required return type was incomplete.

Fortunately I learned about Type Inference and ReturnType in TypeScript.

An example for using ReturnType for infering return type is like this:

const parseData = (datum: string) => {
  return parseInt(datum, 10)
}

type A = ReturnType<typeof parseInt>
  • The typeof will return the type of the function, which is (datum: string) => number.
  • Then the RetunType will capture the return type
  • The result is type A which is number