Open In App

What is the Function type in TypeScript ?

TypeScript is a JavaScript-based programming language with a typed syntax. It provides improved tools of any size. It adds extra syntax to JavaScript. This helps in facilitating a stronger interaction between you and your editor. It also helps in catching the mistakes well in advance. 

It uses type inference to provide powerful tools without the need for extra code. TypeScript may be executed everywhere JavaScript is supported as it can be converted to JavaScript code. 



TypeScript Functions: Functions are the most crucial aspect of JavaScript as it is a functional programming language. Functions are pieces of code that execute specified tasks. They are used to implement object-oriented programming principles like classes, objects, polymorphism, and abstraction. It is used to ensure the reusability and maintainability of the program. Although TypeScript has the idea of classes and modules, functions are still an important aspect of the language.

Function declaration: The name, parameters, and return type of a function are all specified in a function declaration. The function declaration has the following:



 Syntax:

function functionName(arg1, arg2, ... , argN);

Function definition: It includes the actual statements that will be executed. It outlines what should be done and how it should be done. The function definition has the following

 Syntax:

function functionName(arg1, arg2, ... , argN){
// Actual code for execution
}

Function call: A function can be called from anywhere in the application. In both function calling and function definition, the parameter/argument must be the same. We must pass the same number of parameters that the function definition specifies. The function call has the following 

Syntax:

functionName(arg1, arg2, ... , argM);

Types of Functions in TypeScript: There are two types of functions in TypeScript:

1. Named function: A named function is defined as one that is declared and called by its given name. They may include parameters and have return types. 

Syntax:

functionName( [args] ) { }  

Example: 




// Named Function Definition  
function myFunction(x: number, y: number): number {
  return x + y;
}
  
// Function Call  
myFunction(7, 5);  

Output:

12

2. Anonymous Function: An anonymous function is a function without a name. At runtime, these kinds of functions are dynamically defined as an expression. We may save it in a variable and eliminate the requirement for function names. They accept inputs and return outputs in the same way as normal functions do. We may use the variable name to call it when we need it. The functions themselves are contained inside the variable.

Syntax: 

let result = function( [args] ) { }  

Example:




// Anonymous Function  
let myFunction = function (a: number, b: number) : number {  
    return a + b;  
};  
  
// Anonymous Function Call  
console.log(myFuction(7, 5));

Output:

12

Advantage of function: Benefits of functions may include but are not limited to the following:


Article Tags :