Open In App

What is the Function type in TypeScript ?

Improve
Improve
Like Article
Like
Save
Share
Report

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:

  • Named Function
  • Anonymous Function

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: 

Javascript




// 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:

Javascript




// 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:

  • Code Reusability: We can call a function several times without having to rewrite the same code block. The reusability of code saves time and decreases the size of the program.
  • Less coding: Our software is more concise because of the functions. As a result, we don’t need to write a large number of lines of code each time we execute a routine activity.
  • Easy to debug: It makes it simple for the programmer to discover and isolate incorrect data.


Last Updated : 29 Nov, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads