DEV Community

Cover image for How to easily make a type from a function's parameters in TypeScript?
MELVIN GEORGE
MELVIN GEORGE

Posted on • Originally published at melvingeorge.me

How to easily make a type from a function's parameters in TypeScript?

Originally posted here!

To easily make a type from the function's parameters in TypeScript, you can use the Parameters utility type and pass the function type declaration which you need to get the parameter types from as the first type argument.

TL;DR

// a simple function
function sayGreeting(name: string, greeting: string) {
  return `${greeting}, ${name}`;
}

// make type from function's parameters
// using the `Parameters` utility type
type OnlyFunctionParameters = Parameters<typeof sayGreeting>; // [name: string, greeting: string]
Enter fullscreen mode Exit fullscreen mode

Let's say we have a function called sayGreeting that accepts 2 parameters called name and greeting both having the type of string like this,

// a simple function
function sayGreeting(name: string, greeting: string) {
  return `${greeting}, ${name}`;
}
Enter fullscreen mode Exit fullscreen mode

Now to get the type of parameters in the sayGreeting function, we can use the Parameters utility type and then pass the type of the sayGreeting function.

Now you might ask how can we get the type of a function, for that we can use a nifty operator called typeof followed by writing the name of the function. This will automatically infer the function entire type including parameters and the return type. We can then pass this as the first type argument to the Parameters utility type.

It can be done like this,

// a simple function
function sayGreeting(name: string, greeting: string) {
  return `${greeting}, ${name}`;
}

// make type from function's parameters
// using the `Parameters` utility type
type OnlyFunctionParameters = Parameters<typeof sayGreeting>; // [name: string, greeting: string]
Enter fullscreen mode Exit fullscreen mode

Now if you hover over the OnlyFunctionParameters you can see an array of types. You can use this as a type for variables that are of array type in TypeScript.

We have successfully made a type from the function parameters in TypeScript. Yay 🥳!

See the above code live in codesandbox.

That's all 😃!

Feel free to share if you found this useful 😃.


Heroku

Built for developers, by developers.

Whether you're building a simple prototype or a business-critical product, Heroku's fully-managed platform gives you the simplest path to delivering apps quickly — using the tools and languages you already love!

Learn More

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Explore this insightful post in the vibrant DEV Community. Developers from all walks of life are invited to contribute and elevate our shared know-how.

A simple "thank you" could lift spirits—leave your kudos in the comments!

On DEV, passing on wisdom paves our way and unites us. Enjoyed this piece? A brief note of thanks to the writer goes a long way.

Okay