Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • TypeScript Tutorial
  • TS Exercise
  • TS Interview Questions
  • TS Cheat Sheet
  • TS Array
  • TS String
  • TS Object
  • TS Operators
  • TS Projects
  • TS Union Types
  • TS Function
  • TS Class
  • TS Generic
Open In App
Next Article:
TypeScript Call Signatures
Next article icon

TypeScript Call Signatures

Last Updated : 22 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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

  • Allow objects to be invoked as functions while possessing properties.
  • Enhance code flexibility by combining callable behavior with structured data.

Syntax:

type MyCallableObject = {
(parameter1: Type1, parameter2: Type2): ReturnType; // Call signature
propertyName: PropertyType; // Additional property
};

Parameters:

  • MyCallableObject: The name of the type alias for the callable object.
  • (parameter1: Type1, parameter2: Type2): Defines the parameters and their types for the callable aspect.
  • ReturnType: Specifies the return type of the callable function.
  • propertyName: PropertyType: An example of an additional property within the object.

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);      
  • GreetingFunction defines a callable object that takes a string and returns a string.
  • The greet function implements this call signature and includes an additional description property.

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)}`);  
  • Calculator defines a callable object that takes two number parameters and returns a number.
  • The add and multiply functions implement this call signature with specific operations and include an operation property describing the operation.

Output:

Addition: 8
Multiplication: 15

Best Practices for Using TypeScript Call Signatures

  • Clearly Define Parameter and Return Types: Ensure all call signatures specify explicit parameter and return types for better type safety.
  • Include Descriptive Properties: Add properties that provide context or metadata about the callable object to enhance code readability.
  • Use Consistent Naming Conventions: Adopt clear and consistent naming for callable objects and their properties to improve maintainability.

Next Article
TypeScript Call Signatures

A

akshitsaxenaa09
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • Geeks Premier League
  • TypeScript
  • Geeks Premier League 2023

Similar Reads

    TypeScript Construct Signatures
    TypeScript Construct Signatures define the shape of a constructor function, specifying the parameters it expects and the type of object it constructs. They use the new keyword in a type declaration to ensure the correct instantiation of classes or objects.Syntaxtype Constructor = new (param1: Type1,
    3 min read
    TypeScript Interfaces Type
    TypeScript Interfaces Type offers an alternative method for defining an object's type, allowing for a distinct naming approach. Syntax:interface InterfaceName { property1: type1; property2?: type2; readonly property3: type3; // ... method1(): returnType1; method2(): returnType2; // ...}Parameters:in
    2 min read
    TypeScript Optional Parameters in Callbacks
    In TypeScript, optional parameters in callbacks are parameters that may or may not be provided when the callback function is invoked. They are denoted by a ? after the parameter name, allowing for more flexible function calls without requiring all arguments.Syntaxtype MyCallback = (param1: string, p
    2 min read
    TypeScript Assignability of Functions
    In this article, we will explore the concept of assignability of functions in TypeScript. Specifically, we will discuss how functions with a return type of void can be assigned to function types with other return types, including those that return values.Understanding AssignabilityIn TypeScript, fun
    3 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 error
    6 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences