How to Create a Generic Type Alias for a Generic Function in TypeScript ?
Last Updated :
25 Jul, 2024
In TypeScript, it is possible to create a generic type alias for a generic function. A generic type alias provides a descriptive name for a function with generic parameters, making it easier to understand the code. It also makes the code reusable and readable especially when you are dealing with complex type structure.
Using the type keyword
In this approach, we define a type alias using the type keyword. The alias contains the generic function signature, including its parameter and return type, while also making it generic so that it can operate on different types.
Syntax:
type MyFunction<T> = (arg: T) => T;
Example: The below code example will explain the use of the type keyword to create a generic type alias.
JavaScript
type MyFunction<T> = (arg: T) => T;
const double: MyFunction<number> =
(x) => x * 2;
const capitalize: MyFunction<string> =
(str) => str.toUpperCase();
console.log(capitalize("GeeksForGeeks"));
console.log("Workforce:" + double(100));
Output:
GEEKSFORGEEKS
Workforce: 200
Using the interface keyword
In this approach, we create a generic type alias for a function using the interface keyword. TypeScript Interfaces are typically used to define the structure of the object, however, we can use it to describe function signatures as well.
Syntax:
interface MyFunction<T> {
(arg: T): T;
}
Example: The below code will explain the use of interfaces to create a generic type alias for a generic function in TypeScript.
JavaScript
interface MyFunction<T> {
(arg: T): T;
}
const increment: MyFunction<number> =
(num) => num + 1;
const reverse: MyFunction<string> =
(str) => str.split("").reverse().join("");
console.log("Workforce: " + increment(199));
console.log(reverse("GeeksForGeeks"));
Output:
Workforce: 200
skeeGroFskeeG
Using Type Aliases with Function Overloads
In this approach, we use TypeScript’s type aliasing along with function overloads to create a more flexible and descriptive type alias for a generic function. This allows you to define multiple signatures for the same function, enhancing type safety and providing more detailed type information.
Syntax:
type MyFunction<T> = {
(arg: T): T;
additionalInfo?: string;
};
Example: Here’s an example demonstrating how to use a type alias with function overloads
JavaScript
type MyFunction<T> = {
(arg: T): T;
additionalInfo?: string;
};
const double: MyFunction<number> = (x) => x * 2;
double.additionalInfo = "Doubles the input value";
const capitalize: MyFunction<string> = (str) => str.toUpperCase();
capitalize.additionalInfo = "Capitalizes the input string";
console.log(double(11));
console.log(capitalize("world"));
console.log("Double info: ", double.additionalInfo);
console.log("Capitalize info: ", capitalize.additionalInfo);
Output:
22
WORLD
Double info: Doubles the input value
Capitalize info: Capitalizes the input string
Similar Reads
How to Define a Generic Type for an Array in TypeScript ? Generics in TypeScript are a way to write reusable code that can work with different types of data. When it comes to defining generic types for arrays. TypeScript provides flexible options to ensure type safety while allowing for flexibility in the types of elements stored in the array.Table of Cont
3 min read
How to get Argument Types from Function in TypeScript ? In TypeScript, the Parameters utility type allows you to extract the types of a function's parameters. This is particularly useful when you want to work with the types of arguments dynamically, enabling functions that operate on any function's parameter types. In this article, we are going to see ho
2 min read
How to Create TypeScript Generic Function with Safe Type Matching ? In TypeScript, generic functions offer a powerful tool for creating flexible and reusable code that can work with various data types. However, ensuring type safety is crucial to prevent runtime errors and maintain code reliability. These are the following approaches:Table of ContentUsing Type constr
4 min read
TypeScript - Generic Functions and Generic Classes Generics in TypeScript enable functions and classes to work with multiple data types while ensuring type safety. Generic functions provide reusable logic that adapts to various inputs, while generic classes help define flexible and type-safe structures.Generic FunctionsA generic function allows you
2 min read
How to Declare a Function that Throws an Error in TypeScript ? In this article, we are going to declare the functions that throw errors in TypeScript. A function can throw an error if the logic is not correct or you can deliberately declare the function that always throws an error. A function can be created with the different return types with the required type
2 min read
How to Create Arrays of Generic Interfaces in TypeScript ? In TypeScript, managing data structures effectively is crucial for building robust applications. Arrays of generic interfaces provide a powerful mechanism to handle varied data types while maintaining type safety and flexibility. There are various methods for constructing arrays of generic interface
3 min read
How to Make a Generic Sorting Function in TypeScript ? Sorting is a fundamental operation in programming, allowing us to organize data in a specific order for easier manipulation and retrieval. In TypeScript, creating a generic sorting function provides flexibility and reusability across different data types. Table of Content Using Array.prototype.sort(
4 min read
How to create conditional types in TypeScript ? Conditional types in TypeScript enable defining types based on conditions, similar to conditional statements in code. They determine different types of values, making functions adaptable to various input types, and enhancing code flexibility and maintainability. Syntax: We can create conditional typ
3 min read
How to Create a Union Type from Nested Array in TypeScript ? TypeScript developers often encounter scenarios where they need to represent a value that could be one of several different types. TypeScript's union types provide a solution to this problem by allowing variables to hold values of multiple types. However, when dealing with nested arrays, each nested
3 min read
What are type aliases and how to create it in Typescript ? In Typescript, Type aliases give a type a new name. They are similar to interfaces in that they can be used to name primitives and any other kinds that you'd have to define by hand otherwise. Aliasing doesn't truly create a new type; instead, it gives that type a new name. Aliasing a primitive isn't
3 min read