What is the Function type in TypeScript ?
Last Updated :
29 Nov, 2021
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
function myFunction(x: number, y: number): number {
return x + y;
}
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
let myFunction = function (a: number, b: number) : number {
return a + b;
};
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.
Similar Reads
What is Type Annotations in TypeScript ?
TypeScript Type Annotations allow developers to explicitly define the types of variables, functions, and other elements in the program. They help catch mistakes early by ensuring the right data type is used.They make the code easier to understand and follow.Type Annotations help avoid errors and mak
3 min read
What is the Record Type in TypeScript ?
In TypeScript, the Record type is a utility type that represents an object type with keys and values of a specific type. It is often used when you want to define a type for the keys and values of an object. In this article, we will learn what is the Record type in TypeScript. Syntax:RecordKeys: A un
2 min read
TypeScript Functions Type
TypeScript function types define the structure of a function, including its parameter types and return type, ensuring consistent and type-safe usage. Help validate the types of parameters passed to a function.Ensure the function returns the expected type.Improve code clarity and prevent runtime erro
6 min read
What is Type Erasure in TypeScript?
TypeScript is a very mighty superset of JavaScript, which adds static typing to the language and allows developers to catch errors early on as well as write more maintainable code. This being said a TypeScript type system erases type information at compile time (or during the compilation), a phenome
4 min read
What is Type Predicates in Typescript ?
In this article, we are going to learn about the type predicates in Typescript. TypeScript is a statically typed programming language that provides many features to make your code more efficient and robust. Type predicates in TypeScript are functions that return a boolean value and are used to narro
3 min read
What is Parameter Destructuring in TypeScript ?
Parameter destructuring in TypeScript is a way to extract values from objects or arrays passed as function parameters, making it easier to work with their properties or elements directly within the function body. There are several methods through which parameter destructuring is achieved in TypeScri
3 min read
What is the difference between interface and type in TypeScript ?
In TypeScript, both interface and type are used to define the structure of objects, but they differ in flexibility and usage. While interface is extendable and primarily for object shapes, type is more versatile, allowing unions, intersections, and more complex type definitions. Type in TypeScriptTh
4 min read
How to Type an Async Function in TypeScript ?
An asynchronous function allows your code to do multiple things at once. It doesn't block the execution of the rest of your code while waiting for a long task (like reading a file, making an API request) to finish. Instead, it returns a Promise, making it easier to handle operations that might take
3 min read
What is TypeScript Definition Manager ?
What is TypeScript Definition Manager? TypeScript Definition Manager (TSDM) is a type definition manager for TypeScript, the popular JavaScript superset. It is a command line tool (CLI) that helps developers create, maintain, and manage type definitions for TypeScript projects. It is an open-source,
4 min read
What is namespace in Typescript ?
In TypeScript, a namespace is a way to organize code logically and prevent naming conflicts between identifiers. It allows developers to group related functionalities, such as interfaces, classes, functions, and variables, within a dedicated scope. Namespaces are particularly useful for structuring
3 min read