DZone
Web Dev Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Web Dev Zone > How the TypeScript Parameters Type Works

How the TypeScript Parameters Type Works

Let's take a look at how the TypeScript Parameters Utility type works.

Johnny Simpson user avatar by
Johnny Simpson
CORE ·
May. 10, 22 · Web Dev Zone · Tutorial
Like (3)
Save
Tweet
756 Views

Join the DZone community and get the full member experience.

Join For Free

The TypeScript Parameters Type is used to take the parameters or arguments of a function and create a new type based on them. It is quite useful when we know that the input of a Function conforms to a certain type, and we want to replicate that. In this guide, let's look at how the Parameters utility type works.

TypeScript Custom Types

This guide covers custom types. If you're new to custom types, read my guide on custom types here.

How the Parameters Type Works

Imagine you have a function, with a set number of arguments. For example, here is a TypeScript function with two parameters or arguments, a, and b:

TypeScript
 
const myFunction = (a: string, b: string) => {
    return a + b;
}

Let's say we want to run this function. One way is to pass in an array with a tuple type the three dots operator. For example:

TypeScript
 
const myFunction = (a: string, b: string) => {
    return a + b;
}

let passArray:[string, string] = [ 'hello ', 'world' ]

// Returns 'hello world'
myFunction(...passArray);

Here we define a tuple, which is simply [string, string], which we can pass into myFunction, satisfying both the a and b arguments.

That works fine, but what if the arguments for myFunction change? This is especially likely to happen if myFunction is coming from a third-party script. Then we would have to not only update our function but remember to update our tuple type which we pass in. We'd also need to stay up to date with the package myFunction is in, in case it changes.

Instead, if we want to ensure they always match, we can use Parameters to produce the same type. This will create a tuple type of the arguments, instead of us having to define it manually:

TypeScript
 
type myType = Parameters<typeof myFunction>
// Equivalent to a tuple type of:
// type myType = [ a: string, b: string ]

This saves us some hassle in defining custom types since we can now pass anything of the type myType into myFunction with little fear of error:

TypeScript
 
const myFunction = (a: string, b: string) => {
    return a + b;
}
type myType = Parameters<typeof myFunction>
let myArray:myType = [ 'hello ', 'world' ];
myFunction(...myArray)

Typing Specific Parameters With the Parameter Utility Type

Parameter types are also quite flexible, and allow us to define more than just the full set of arguments. For example, if we wanted to only match the type of the first argument in our function myFunction, we can reference that like a simple array, by adding [0]. The following will match the type of a, but if we wanted to match the type of b we could use [1]:

TypeScript
 
type myType = Parameters<typeof myFunction>[0]
// Equivalent of 'string'

That way, we could define custom types for each argument if we have to in our code. For example, here we define two custom types for the first and second parameters of the function, and pass them both into our function:

TypeScript
 
const myFunction = (a: string, b: string) => {
    return a + b;
}

type aType = Parameters<typeof myFunction>[0]
type bType = Parameters<typeof myFunction>[1]

let a:aType = 'hello '
let b:bType = 'world'

myFunction(a, b)

More Fun With Parameters

Since Parameters converts the type of arguments to a new type, we can also pass a function directly into it. The below will produce a type [ a: string, b: number ]. This is less useful than taking the parameters from a specific function, but can serve purposes in certain situations:

TypeScript
 
type anotherType = Parameters<(a: string, b: number) => void>
TypeScript Data Types

Published at DZone with permission of Johnny Simpson, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • A Guide to Events in Vue
  • Toying With Kotlin’s Context Receivers
  • Usage of Java Streams and Lambdas in Selenium WebDriver
  • Top 20 Git Commands With Examples

Comments

Web Dev Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo