DZone
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
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • TypeScript: Useful Features
  • Mule 4 DataWeave(1.x) Script To Resolve Wildcard Dynamically
  • Why Round-Robin Won't Save You: Load Balancing Challenges in Data Streaming Services With Heterogeneous Traffic
  • Ingesting Fixed-Width Mainframe Files Into Delta Lake: The Details Nobody Writes Down

Trending

  • LLM Integration in Enterprise Applications: A Practical Guide
  • Key Takeaways From Integrating a RAG Application With LangSmith
  • The Hidden Latency of Autoscaling
  • MuleSoft MCP and A2A in Production: What 17 Recipes Reveal
  1. DZone
  2. Data Engineering
  3. Data
  4. 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.

By 
Johnny Simpson user avatar
Johnny Simpson
·
Updated May. 10, 22 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
2.3K 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. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • TypeScript: Useful Features
  • Mule 4 DataWeave(1.x) Script To Resolve Wildcard Dynamically
  • Why Round-Robin Won't Save You: Load Balancing Challenges in Data Streaming Services With Heterogeneous Traffic
  • Ingesting Fixed-Width Mainframe Files Into Delta Lake: The Details Nobody Writes Down

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook