Kaio Duarte
Kaio Duarte

Reputation: 133

Typescript - Extract return type from inner function

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; }

Upvotes: 3

Views: 610

Answers (1)

futur
futur

Reputation: 1843

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.

Upvotes: 5

Related Questions