#3312Easy

Parameters

Extract function parameter types with TypeScript. Master the infer keyword and function type manipulation in this easy-level challenge on TypeScriptPro.

Extract function parameter types like a pro!

In this easy-level challenge, you'll implement TypeScript's built-in Parameters<T> utility type from scratch. This type extracts the parameter types from a function type and returns them as a tuple. It's an essential tool for working with functions at the type level.

You'll learn how to use conditional types with infer to capture function parameters, a technique that opens the door to powerful function type manipulations in TypeScript.

Challenge Instructions: Parameters

Easy

Implement the built-in Parameters<T> generic without using it.

For example:

const foo = (arg1: string, arg2: number): void => {}
 
type FunctionParamsType = MyParameters<typeof foo> // [arg1: string, arg2: number]

Change the following code to make the test cases pass (no type check errors).

Loading...

Video Walkthrough

Detailed Explanation

The Parameters type uses conditional types with infer to extract function parameters.

type MyParameters<T extends (...args: any[]) => any> = T extends (
  ...args: infer Params
) => any
  ? Params
  : false

Breaking it down:

This pattern is fundamental for introspecting function types and is used extensively in TypeScript's utility types and advanced type programming.

This challenge is originally from here.

Share this challenge

Related Challenges

Learn the Concepts

Become a TypeScript Pro

Track your progress through 100+ hands-on challenges. Free, sign in with GitHub.

Or start solving right away: explore all TypeScript challenges