Last updated: Feb 27, 2024
Reading timeยท3 min
Use the Parameters
utility type to get the type of a function's arguments.
The Parameters
utility type constructs a tuple type from the types used in
the function's parameters.
function sum(a: number, b: string): string { return a + b; } // ๐๏ธ type SumParams = [a: number, b: string] type SumParams = Parameters<typeof sum>; // ๐๏ธ type FirstParam = number type FirstParam = SumParams[0]; // ๐๏ธ type SecondParam = string type SecondParam = SumParams[1];
The Parameters utility type constructs a tuple type from the types used in the function's parameters.
Here is an example of how you would use the Parameters
utility type for 2
functions that take the same object as a parameter.
type Person = { name: string; age: number; country: string; }; function getObj(obj: Person): Person { return obj; } function getObj2(obj: Parameters<typeof getObj>[0]) { return obj; } console.log(getObj2({ name: 'Bobby Hadz', age: 30, country: 'Chile' }));
The getObj2()
function takes an object of the same type as the one the
getObj()
function takes.
After we get the tuple of parameter types, we have to access the element at
index 0
to get the type of the object.
type Person = { name: string; age: number; country: string; }; function getObj(obj: Person): Person { return obj; } // ๐๏ธ type GetObjParams = [obj: Person] type GetObjParams = Parameters<typeof getObj>
This is because the Parameters
utility type returns a
tuple, even if the function takes a
single parameter.
Use the ConstructorParameters
utility type to get the parameters type of a
class constructor in TypeScript.
class Person { constructor(public name: string, public age: number, public country: string) { this.name = name; this.age = age; this.country = country; } } // ๐๏ธ type PersonParamsType = [name: string, age: number, country: string] type PersonParamsType = ConstructorParameters<typeof Person>; // ๐๏ธ type First = string type First = PersonParamsType[0]; // ๐๏ธ type Second = number type Second = PersonParamsType[1];
We used the ConstructorParameters utility type to get a tuple type of all of the constructor function's parameter types.
If you need to access the type of a specific, parameter, e.g. the first one, you can use bracket notation and access the element at the specific index.
class Person { constructor(public name: string, public age: number, public country: string) { this.name = name; this.age = age; this.country = country; } } // ๐๏ธ type PersonParamsType = [name: string, age: number, country: string] type PersonParamsType = ConstructorParameters<typeof Person>; // ๐๏ธ type First = string type First = PersonParamsType[0]; // ๐๏ธ type Second = number type Second = PersonParamsType[1]; // ๐๏ธ type Third = string type Third = PersonParamsType[2];
The indexes of the tuple are zero-based, just like with arrays.
Note that the ConstructorParameters
utility type would return a tuple
containing the parameter types, even if the constructor function takes a single
parameter.
class Person { name: string; age: number; country: string; constructor({ name, age, country, }: { name: string; age: number; country: string; }) { this.name = name; this.age = age; this.country = country; } } // ๐๏ธ type PersonParamsType = [{ // name: string; // age: number; // country: string; // }] type PersonParamsType = ConstructorParameters<typeof Person>; // ๐๏ธ type First = { // name: string; // age: number; // country: string; // } type First = PersonParamsType[0];
The class in the example takes a single parameter - an object. However, the
ConstructorParameters
still returns a tuple containing the object.
If you need to access the type of the object, you need to access the tuple
element at index 0
.
If you need to pass a function as a parameter, check out the following article.