Skip to the solution
MediumFrontend9 min read

What do ReturnType and Parameters do?

1.0k views

The problem

Question presented to candidate: "What do ReturnType and Parameters do? When have you used them, and what are their limits — say with overloads or generic functions?"

What a strong answer should cover:

  • ReturnType<F> gives the return type of a function type; Parameters<F> gives its parameter types as a (labelled) tuple; both need a function type, so you pass typeof fn
  • They are conditional types with infer (F extends (...args: any) => infer R ? R : any)
  • Uses: name a type nobody exported, type wrappers and decorators that keep a signature ((...args: Parameters<F>) => ReturnType<F>), forward arguments; ConstructorParameters and InstanceType for classes
  • Async functions: ReturnType is the Promise; combine with Awaited for the resolved value
  • Limits: overloads use only the last signature, generic functions get their constraint (unknown) unless you pin the type with an instantiation expression, and non-functions are rejected by the constraint

Code / implementation expected: Optional — a withLog wrapper using both, or MyReturnType with infer.

  • returntype
  • parameters

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

9 min read · 10 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

TypeScriptDeriving types from functions and typing a wrapper
TypeScriptOverloads, generic functions, methods and other edge cases

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 20 of 81 in TypeScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextMedium

What are generics and why are they useful?

Continue