A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/typescript/typescript-call-signatures/ below:

TypeScript Call Signatures - GeeksforGeeks

TypeScript Call Signatures

Last Updated : 22 Jan, 2025

TypeScript call signatures define the parameter types and return types for function-like objects, enabling the creation of callable entities with additional properties.

Syntax:
type MyCallableObject = {
(parameter1: Type1, parameter2: Type2): ReturnType; // Call signature
propertyName: PropertyType; // Additional property
};
Parameters: Greeting Function Using Call Signature TypeScript
type GreetingFunction = {
    (name: string): string; // Call signature
    description: string;    // Additional property
};

const greet: GreetingFunction = (name: string) => {
    return `Hello, ${name}!`;
};

greet.description = "A function to greet users";

console.log(greet("Alice"));          
console.log(greet.description);     

Output:

Hello, Alice!
A function to greet users
Calculator Using Call Signature TypeScript
type Calculator = {
    (a: number, b: number): number; // Call signature
    operation: string;              // Additional property
};

const add: Calculator = (a: number, b: number) => a + b;
add.operation = "Addition";

const multiply: Calculator = (a: number, b: number) => a * b;
multiply.operation = "Multiplication";

console.log(`${add.operation}: ${add(5, 3)}`);        
console.log(`${multiply.operation}: ${multiply(5, 3)}`); 

Output:

Addition: 8
Multiplication: 15
Best Practices for Using TypeScript Call Signatures

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4