TypeScript: Type Definition for Functions

In typescript, we have to define types for a function definition. It may include type definitions for function parameters and type for the function return value. Simple we can say we have to define a type for inputs and output of the function.

Lets understand it better with the help of an example.

Types for function – TypeScript Examples

Example 1 :

function add(a: number, b:number): number {
  return a + b;
}

In the above example we have type number defined for both input variables a & b. Also we have another type number defined as the type for the return value (output of the function.

Example 2:

function print(value: any){
 console.log(value);
}

In the above example input value can be of any type. Also there is no return type.

About the Author: smartcoder

You might like

Leave a Reply

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