| by Arround The Web | No comments

How to Specify the Types of a Function in TypeScript

Functions are the most important components of JavaScript that perform specific tasks on the objects. They allow the users to reuse the specific code defined in it to enhance its maintainability. In TypeScript, the functions are categorized according to their arguments/parameters and return types. It helps to notify the user that a particular function is defined with a specific type and any other data type parameter can’t be used in it.

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:

function Time(): number {

return new Date().getTime();

}

console.log(Time());

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:

tsc main.ts //Compile

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:

function add(a:number, b:number):number

{

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

tsc main.ts

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

functionName( [args] ) { }

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

let result = function( [args] ) { }

Now, let’s demonstrate the discussed function practically:

let myFunc = function (x: number, y: number) : number {

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

tsc main.ts

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.

Share Button

Source: linuxhint.com

Leave a Reply