What are Function Types in TypeScript?
Function Types are one of the important features of TypeScript. They are used to define the types of functions, parameters and return types. It enables the developer to specify the signature of a function and its type, so that the TypeScript compiler can check if the function has been invoked correctly.
In TypeScript, when we define a function, we also define its type. The type of a function is made up of its parameters and return type.
Function Type Syntax in TypeScript
The syntax for a function type is very similar to the syntax for defining a function in TypeScript. Here is an example:
“`
function type syntax example:
let add: (x: number, y: number) => number;
“`
In the above example, we have defined a type for a function called `add`. This type specifies that the function takes two parameters, both of which are of type `number`, and returns a value of type `number`.
Assigning Function Types to Variables
Function Types can be easily assigned to variables, and this is where they become really useful. Here is an example:
“`
Assigning function types to variables:
let add: (x: number, y: number) => number;
add = function(x: number, y: number): number {
return x + y;
}
“`
In the above example, we have assigned the `add` function type to a variable called `add`. We have then defined a function that meets the signature of this type. The TypeScript compiler will be able to check that this function meets the expected signature since `add` has to be a function taking two `number` parameters and returning a `number` result.
Function Types as Parameters
Function Types can also be used as parameters for other functions. Here is an example:
“`
Function types as parameters:
function calculate(operation: (x: number, y: number) => number, x: number, y: number): number {
return operation(x, y);
}
let result = calculate(function(x, y) { return x * y }, 10, 2);
“`
In the above example, the `calculate` function takes two `number` parameters and a function parameter called `operation`. The `operation` parameter is of type `(x: number, y: number) => number`, which means it is a function that takes two `number` parameters and returns a `number`.
We can pass any function that meets this signature as the `operation` parameter. In this example, we have passed an anonymous function that multiplies `x` and `y` together.
The `calculate` function then calls the `operation` function, passing in the two `number` parameters `x` and `y`. The result of this operation is returned by the `calculate` function.
Function Types and Optional Parameters
Function Types can also be used with optional parameters. Here is an example:
“`
Function types and optional parameters:
let calculate: (x: number, y: number, z?: number) => number;
calculate = function(x: number, y: number, z?: number): number {
if (z) {
return x * y * z;
} else {
return x * y;
}
}
“`
In the above example, we have defined a function type called `calculate`. This function type takes two parameters of type `number`, and an optional third parameter also of type `number`.
We have then defined a function that meets this type signature. This function takes two required parameters and one optional parameter. If the optional parameter is passed in, it multiplies all three parameters together. If it is not passed in, it multiplies the first two parameters together.
Lambda Expressions
Lambda expressions are a shorthand way of defining functions in TypeScript. They are a more concise way of writing functions and are often used in conjunction with function types. Here is an example:
“`
Lambda expressions:
let add: (x: number, y: number) => number = (x, y) => x + y;
“`
In this example, we have defined a function type called `add`. We have then used a lambda expression to define a function that meets this type signature. The lambda expression is `(x, y) => x + y`, which simply adds `x` and `y` together.
FAQs
What are function types?
Function Types are used to define the types of functions, parameters and return types in TypeScript. It enables the developer to specify the signature of a function and its type, so that the TypeScript compiler can check if the function has been invoked correctly.
How do I assign a function type to a variable in TypeScript?
You can assign a function type to a variable in TypeScript by using the following syntax:
“`
let add: (x: number, y: number) => number;
“`
In this example, we have defined a type for a function called `add`. This type specifies that the function takes two parameters, both of which are of type `number`, and returns a value of type `number`.
Can function types be used as parameters?
Function Types can be used as parameters for other functions in TypeScript.
How do function types work with optional parameters?
Function Types can be used with optional parameters in TypeScript. Here is an example:
“`
let calculate: (x: number, y: number, z?: number) => number;
calculate = function(x: number, y: number, z?: number): number {
if (z) {
return x * y * z;
} else {
return x * y;
}
}
“`
In the above example, we have defined a function type called `calculate`. This function type takes two parameters of type `number`, and an optional third parameter also of type `number`.
We have then defined a function that meets this type signature. This function takes two required parameters and one optional parameter. If the optional parameter is passed in, it multiplies all three parameters together. If it is not passed in, it multiplies the first two parameters together.