TypeScript Function Type Expressions
Last Updated :
31 Oct, 2023
In this article, we are going to learn about TypeScript Function Type Expressions in Typescript. TypeScript is a popular programming language used for building scalable and robust applications. In TypeScript, a function type expression represents the type of a function, including its parameter types and return type. Function type expressions can be used to define and annotate the types of functions, which helps in enforcing type safety and providing better code documentation. These types are syntactically similar to arrow functions.
Syntax:
(parameter1: type1, parameter2: type2, ...) => returnType
Where-
- (parameter1: type1, parameter2: type2, ...): This part defines the parameters that the function expects. Each parameter is followed by a colon: and its corresponding type. You can specify multiple parameters separated by commas.
- =>: The fat arrow => separates the parameter list from the return type.
- returnType: This specifies the type of the value that the function returns
Example 1: In this example, We define a function type expression called MathOperation using the arrow function-like syntax. It specifies that any function of this type should take two numbers (a and b) as parameters and return a number. We then create two functions, add and subtract, that conform to the MathOperation type. Both functions take two numbers and return the result of addition and subtraction, respectively.
JavaScript
// Define a function type expression
// using arrow function-like syntax
type MathOperation = (a: number, b: number) => number;
// Create a function that conforms
// to the MathOperation type
const add: MathOperation = (a, b) => a + b;
const subtract: MathOperation = (a, b) => a - b;
// Use the defined functions
console.log(add(5, 3));
console.log(subtract(10, 4));
Output:

Example 2: In this example,We define a function type expression called StringManipulator using the arrow function-like syntax. It specifies that any function of this type should take a string (input) as a parameter and return a string.We create two functions, capitalize and reverse, that conform to the StringManipulator type. The capitalize function capitalizes the first letter of a string, and the reverse function reverses the characters in a string.
JavaScript
// Define a function type expression
// using arrow function-like syntax
type StringManipulator = (input: string) => string;
// Create a function that conforms
// to the StringManipulator type
const capitalize: StringManipulator = (input) => {
return input.charAt(0).toUpperCase() + input.slice(1);
};
const reverse: StringManipulator = (input) => {
return input.split('').reverse().join('');
};
// Use the defined functions
const originalString = 'GeeksforGeeks is a Computer Science Portal';
const capitalizedString = capitalize(originalString);
console.log(capitalizedString);
const reversedString = reverse(originalString);
console.log(reversedString);
Output:

Conclusion: In this article, we learnt that Function type expressions can be used to define and annotate the types of functions, which helps in enforcing type safety and providing better code documentation. These types are syntactically similar to arrow functions.
Similar Reads
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
TypeScript Anonymous Functions Type In TypeScript, an Anonymous Function Type defines a function without a specific name, specifying parameters and return types. This allows for flexible and reusable function definitions, enabling the assignment of functions to variables and the use of type annotations for parameters and return values
3 min read
TypeScript Assertion functions TypeScript Assertion functions are nothing but the functions that allow us to mainly create the custom type guards that assert the type of a value on our specific criteria. The most suitable use of Assertion Functions is when we are working with the more complex data structures. Syntax:function asse
3 min read
TypeScript Assertions Type TypeScript Assertions Type, also known as Type Assertion, is a feature that lets developers manually override the inferred or expected type of a value, providing more control over type checking in situations where TypeScript's automatic type inference may not be sufficient.Syntaxlet variableName: As
2 min read
Explain Type assertions in TypeScript In TypeScript, type assertions allow developers to override the compiler's inferred type, informing it of the specific type of a value. Type assertions are purely compile-time constructs and do not alter the runtime behavior of the code. They are particularly useful when interfacing with APIs or thi
3 min read