Interface with Function Signature

An interface defines a function signature, and you use it to declare the arrow function.

Example: An interface defines a function signature, and you use it to declare the arrow function.

Javascript
interface MathOperation {
  (a: number, b: number): number;
}

const subtract: MathOperation = (a, b) => a - b;

console.log(subtract(8, 3)); // Output: 5

Output:

5

How to Specify Return Type in TypeScript Arrow Functions ?

To Specify the return type in the TypeScript arrow function, we have multiple approaches. In this article, we are going to learn how to Specify the type in the TypeScript arrow function.

Below are the approaches used to Specify the return type in the TypeScript arrow function:

Table of Content

  • Explicit Return Type Annotation
  • Type Inference
  • Function Declaration
  • Interface with Function Signature
  • Using Type Alias for Function Signature

Similar Reads

Explicit Return Type Annotation

You explicitly annotate the return type after the parameter list using ‘:’...

Type Inference

TypeScript infers the return type based on the return statement when the function body is a single expression....

Function Declaration

You can specify the return type when declaring the function separately....

Interface with Function Signature

An interface defines a function signature, and you use it to declare the arrow function....

Using Type Alias for Function Signature

Type aliases in TypeScript provide a way to give a name to a type. You can use type aliases to define the signature of the function and then use it to specify the return type of the arrow function....