An âarrowâ function is utilized to write the function expression in a concise and shorter way. By default, it acts as a private function that cannot be used globally but sometimes the user wants to make it generic to use it globally in the source code.
This post explains the possible approaches for making an arrow function generic in TypeScript.
How To Make An Arrow Function Generic in TypeScript?
To make an arrow function generic the user needs to specify the generic parameter before it with the help of the âTâ placeholder enclosed in âarrow<>â brackets. This placeholder denotes all the data types that the user wants to assign to the specified parameters of an arrow function. It assigns to an argument in the place of its type such as (argument: T).
Letâs see its practical implementation.
Example 1: Make an Arrow Function Generic
This example passes the generic parameter to an arrow function to make it generic.
Code
console.log(input);
};
output<string>('Linuxhint');
output<number>(12345);
output<Boolean>(true);
In the above code block:
- The âoutputâ variable defines an arrow function âvoidâ having a generic parameter.
- In this function, the âlog()â method is applied to display the âvalueâ parameter output.
- Next, the âoutputâ variable specifies the âinputâ parameter values of different data types. It does not produce an error because the âinputâ parameter is generic and accepts values of all data types.
Output
node main.js // Run .js File
It can be seen that the terminal successfully displays different data types values because the given arrow function is defined as generic.
Example 2: Make an Arrow Function Generic For Limited Types
This example makes an arrow function generic that only allows certain types of class/interface to be passed.
Code
name: string;
gender: () => void;
}
class User implements Person {
name = 'Ali';
gender(): void {
console.log('male')
}
}
const output = <T extends Person>(value: T): void => {
console.log(value);
};
output(new User());
In the above code lines:
- The interface âPersonâ has a property âgenderâ that is assigned to an arrow function âvoidâ.
- Next, the class âUserâ applies the âPersonâ interface. The âimplementâ keyword allows the âUserâ class to use the properties of the âPersonâ interface.
- The âUserâ class uses the âgenderâ property with the âvoidâ function definition. In the âvoidâ function definition, the âlog()â method is applied to display the âgenderâ property value.
- Now, the âoutputâ variable passes the generic parameter that extends the âPersonâ interface before the âvoidâ arrow function expression.
- In its definition, the âlog()â method is utilized to display the given generic parameter value.
- Lastly, the âoutputâ variable specifies the âUserâ class constructor as an argument of the arrow function.
Output
node main.js // Run .js File
It is observed that the terminal only shows the ânameâ property value note the âgenderâ of the âPersonâ interface extended in the âUserâ class.
Note: As seen in all examples of this guide, the âtrailing commaâ is specified with a generic parameter because it is necessary while working in .tsx files. Otherwise, the â.tsâ file does not generate a syntax error if the user does not specify it with the generic parameter.
Conclusion
In TypeScript the user can make an arrow function generic by passing the âgenericâ parameters in it. The generic parameters refer to the different data types that can be specified with the help of the âTâ placeholder enclosed in âarrow<>â brackets. Apart from all data types, the user can also limit the data types of generic parameters by using generic constraints. This post practically explained the possible approaches for making an arrow function generic in TypeScript.