#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).

ChallengeSolution
/* _____________ Your Code Here _____________ */

type MyParameters<T extends (...args: any[]) => any> = any

/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '../helpers'

function foo(arg1: string, arg2: number): void {}
function bar(arg1: boolean, arg2: { a: 'A' }): void {}
function baz(): void {}

type cases = [
  Expect<Equal<MyParameters<typeof foo>, [string, number]>>,
  Expect<Equal<MyParameters<typeof bar>, [boolean, { a: 'A' }]>>,
  Expect<Equal<MyParameters<typeof baz>, []>>,
]

Pro Challenge

Unlock 102+ medium, hard, and extreme challenges to master advanced TypeScript.

One-time payment. Lifetime access.

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

Learn the Concepts