TypeScript Call Signatures
Last Updated :
08 Nov, 2023
TypeScript Call Signatures within an object type to describe objects that are callable, meaning they can be invoked like functions, but they may also have properties. This is a powerful feature that allows you to create objects that act as both functions and containers for data. These objects are often referred to as “callable objects.”
Syntax:
type MyCallableObject = {
(parameter1: Type1, parameter2: Type2, ...): ReturnType; // Call signature
propertyName: PropertyType; // Property
};
Parameters:
- MyCallableObject: This is the name of the object type you are defining. You can replace it with your desired type name.
- ( and ): These parentheses enclose the call signature definition, specifying the parameters and return type of the callable object.
- parameter1, parameter2, …: These are the names of the parameters that the callable object function expects, each followed by a colon and their respective types (e.g., Type1, Type2, …).
- ReturnType: This is the type that the callable object function is expected to return when it is invoked.
- propertyName: This is an example of a property that can be included in the object type. You can have any number of properties in your object type, and each one is followed by a colon and its type (e.g. PropertyType).
Example 1: In this example, We define the object type, which includes a call signature that specifies the callable object should accept one parameter (string) and return a string. It also includes a property named propertyName of type string. We create an object myObject of type objects by implementing the call signature with a function that concatenates the parameters.
Javascript
type objects = {
(param: string): string;
propertyName: string;
};
const myObject: objects = (param) => {
return `Received: ${param} `;
};
myObject.propertyName = "Computer Science Portal" ;
const result = myObject( "GeeksforGeeks" );
console.log(result);
console.log(myObject.propertyName);
|
Output:
Example 2: In this example, We define the Calculator type, which includes a call signature for operation(taking two numbers and returning a number) and a property named operation to describe the operation. We create objects of type Calculator, each implementing the call signature with a specific mathematical operation and setting the operation property accordingly.
Javascript
type Calculator = {
(a: number, b: number): number;
operation: string;
};
const add: Calculator = (a, b) => a + b;
add.operation = "Addition" ;
const subtract: Calculator = (a, b) => a - b;
subtract.operation = "Subtraction" ;
const divide: Calculator = (a, b) => a / b;
divide.operation = "Divide" ;
const multiply: Calculator = (a, b) => a * b;
multiply.operation = "Multiply" ;
const sum = add(5, 3);
const difference = subtract(8, 2);
const div = divide(6, 3);
const mul = multiply(4, 4);
console.log(`${add.operation}: ${sum}`);
console.log(`${subtract.operation}: ${difference}`);
console.log(`${divide.operation}: ${div}`);
console.log(`${multiply.operation}: ${mul}`);
|
Output:
Share your thoughts in the comments
Please Login to comment...