03 · TypeScript Type Gymnastics
mediumcodingtypescript~10 min

Extract Function Arguments with infer

Objective: Use `infer` inside conditional types to extract parts of a type.

Implement ArgsOf<F> (the tuple of parameters) and LastArg<F> (the type of the last parameter). Your code is type-checked with strict mode against hidden assertions. Fix every compiler error and make the assertions pass.

  • ArgsOf<F> is the parameter tuple of function type F, and never for non-functions.
  • LastArg<F> is the type of the last parameter, and never when there are none.
LastArg<(a: string, b: number) => void>
→ number
TypeScript (strict)

⌘/Ctrl + Enter to type-check · Esc then Tab leaves the editor

Output
> Nothing has run yet.
Interview follow-up questions
  • How does the built-in Parameters<T> differ from yours?
  • How do overloaded functions behave with infer?
  • How would you get the first argument?