Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript - Extract return type from inner function

Tags:

typescript

Is it possible to get the return type of a function that returns another function?

const creator = (deps: CreatorDependencies) => () => {
  return {
    foo: 1,
    bar: 2,
  };
}

I expect to get this

{ foo: number; bar: number; }
like image 401
Kaio Duarte Avatar asked Jun 11 '26 22:06

Kaio Duarte


1 Answers

This is a use case for TypeScript's ReturnType<Type> utility type.

type CreatorReturn = ReturnType<ReturnType<typeof creator>>

ReturnType<typeof creator> gives you the type of the function returning your object, and wrapping it in another ReturnType<> gives you the object type needed.

like image 143
futur Avatar answered Jun 13 '26 19:06

futur