How to Specify the Types of a Function in TypeScript
This guide explains how the type of a function can specify in TypeScript.
How to Specify the “Type” of a Function in TypeScript?
The “type” keyword corresponds to the type of the function’s parameters/arguments or its return value. Once the type is set with the parameters, then the user can not add any other type of value in it.
Let’s start with the first example.
Example 1: Specify the Function’s Return Type
This first example specifies the return type of the given function that restricts that function to only return the value of the specified return type.
Code
Use these lines of code in the file having the “.ts” extension:
In the above lines of code:
- The “Time()” function specifies the “number” data type as the return type of this function.
- This “return” statement uses the “date” object linked with the “getTime()” method to return the date and time in milliseconds as a “number”.
- Lastly, invoke the defined function.
Output
Compile the “.ts” file using the “tsc” compiler and then run the compiled “.js” file:
node main.js //Run
As seen, the “Time()” function retrieves the specified numeric value as its return type is “number”.
Example 2: Specify the Function’s Parameters Types
This example specifies the type of functions’ parameters to restrict them to not accept any other data type value:
{
return a+b;
}
console.log("Sum is:" + add(10,20));
In this code:
- The “add()” function takes two parameters “a and b” of type “number”.
- This function returns the numeric value as a result of the specified arithmetic operation i.e., “a+b”.
- The “console.log()” method calls the “add()” function passing the stated argument values as “numbers”.
Output
node main.js
Here, the output shows the sum of specified numbers successfully.
Example 3: Types of Functions in TypeScript and Specifying Their Types
In TypeScript, the functions are categorized into two types: “Named” and “Anonymous”.
Named Function
The “Named” function is declared by its given name. This function may contain the function’s parameters type or the return type. Its demo can be overviewed in “Example 2”.
Syntax
Anonymous Function
The “Anonymous” function is assigned to a variable that defines it dynamically as an expression at run time. It works the same as the simple/function. The user can call it using the variable name in which it is assigned to invoke the function’s functionalities.
Syntax
Now, let’s demonstrate the discussed function practically:
return x*y;
};
console.log(myFunc(10, 6));
In the above lines of code:
- The “myFunc” variable defines a function having parameters (with their types), and the return type.
- The “return” statement returns the multiplication of the passed values.
- The “console.log()” method calls the “anonymous function” with the help of its assigned variable “myFunc” by passing the stated values as arguments.
Output
node main.js
Here, the output returns the “numeric” type value since the function’s return type is “number”.
Conclusion
In TypeScript, the “type” of a function refers to the function’s parameters or return value based on the built-in data types such that only the specified values are supported by the function. This guide briefly explained how to specify the “type” of a function using TypeScript.
Source: linuxhint.com