Warning: session_start(): open(/tmp/sess_e9fbc094a8a64d0f373d888ef6948e07, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How To Get The Type Of A Function's Arguments In TypeScript - LearnShareIT

How To Get The Type Of A Function’s Arguments In TypeScript

Get the Type of a Function’s Arguments in TypeScript

Knowing how to get the type of a function’s arguments in TypeScript will be very helpful when working with more than one function. So how to do it? Let’s go into detail now.

Get the type of a function’s arguments in TypeScript

Use Parameters<Type>

Typescript provided us with the Parameters utility type, which will help you to return a tuple type of parameters in your function.

Syntax:

parameters<Type>

Example:

// type parameterType = [string, number, [], {}]
type parameterType = Parameters<
    (para1: string, para2: number, para3: number[], para4: {}) => void
>;

Here I use the Parameters utility type to get all parameter types of the arrow function that I pass inside. Then I get a tuple of the type of that arrow function’s parameter. We can also get each element by passing the index inside square brackets with the tuple type.

Example:

// type parameterType = [string, number, [], {}]
type parameterType = Parameters<
    (para1: string, para2: number, para3: number[], para4: {}) => void
>;

type para1Type = parameterType[0]; // string
type para2Type = parameterType[1]; // number
type para3Type = parameterType[2]; // []
type para4Type = parameterType[3]; // {}

If you have two functions and the second function want to get all type of parameters of the first function, you can apply this way.

Example:

// type parameterType = [string, number, [], {}]
type parameterType = Parameters<
  (
    para1: string,
    para2: number,
    para3: number[],
    para4: { freetime: string }
  ) => void
>;

type para1Type = parameterType[0]; // string
type para2Type = parameterType[1]; // number
type para3Type = parameterType[2]; // []
type para4Type = parameterType[3]; // {}

const logInfor = (
    name: para1Type,
    age: para2Type,
    id: para3Type,
    favourites: para4Type
) => {
    console.log("name: " + name);
    console.log("age: " + age);
    console.log("id: " + id);
    console.log("favourites: " + favourites.freetime);
};

logInfor("James", 28, [15, 1, 5], {
    freetime: "read book, watching movie , walking",
});

Output:

[LOG]: "name: James" 
[LOG]: "age: 28" 
[LOG]: "id: 15,1,5" 
[LOG]: "favourites: reading books, watching the movie,
walking"

Use with the type of argument of an already function

You can also get the type of argument of an already function.

Example:

// type parameterType = [string, number, [], {}]
const anFunction = (
    para1: string,
    para2: number,
    para3: number[],
    para4: { freetime: string }
) => {
    console.log("done");
};

type parameterType = Parameters<typeof anFunction>;
type para1Type = parameterType[0]; // string
type para2Type = parameterType[1]; // number
type para3Type = parameterType[2]; // []
type para4Type = parameterType[3]; // {}  

To get the parameters’ type of already function, you need to use the typeof type of operator.

Summary

In this article, I showed and explained very detail for you how to get the type of a function’s arguments in TypeScript. You can use the Parameters utility type to do it. If you have any problems, please comment below. I will answer as possible. Thanks for reading!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *