TypeScript Working with Generic Type Variables Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report In TypeScript, working with generic type variables is a powerful feature that allows you to create functions and classes that can work with a variety of data types while maintaining type safety. Note: When you declare a generic function or class, you introduce a type variable enclosed in angle brackets (<>). This type variable serves as a placeholder for a specific type that will be provided when the function or class is used. This enables you to create more flexible and reusable code. Syntax:function identity<Type>(arg: Type): Type {// Body of function}Parameters:Type is the type of datatype.Example 1: In this example, we have created an identity function that is a generic function. we will first pass a string to this and then pass a number to this. Our function is able to infer the type of variable and print it. We are also printing its type using the 'typeof()' function. JavaScript function identity<Type>(arg: Type): Type { return arg; } // The result is of type string let result = identity("GeeksforGeeks"); // The result2 is of type number let result2 = identity(42); console.log(result + " " + typeof result); console.log(result2 + " " + typeof result2); Output: Example 2: In this example, we will see what if we want to log the length of the argument arg to the console with each call. This will give us error. Make note that these type variables stand in for any and all types, so someone using this function could have passed in a number instead, which does not have a .length member. so, instead of Type we have to use Type[] as a parameter. JavaScript function identity<Type>(arg: Type): Type { return arg.length; } let result = identity(44); console.log(result) Output: Conclusion: In this article we learnt that about working with generic types in typescript. In TypeScript, working with generic type variables is a powerful feature that allows you to create functions and classes that can work with a variety of data types while maintaining type safety. Comment More infoAdvertise with us Next Article TypeScript Generic Types 21mcsrltd Follow Improve Article Tags : TypeScript Geeks Premier League 2023 Similar Reads TypeScript Working with Constrained Values TypeScript Working with Constrained Values specifies how can we deal with Constrained Values. Generic Constraints are used to specify limits to the types that can be used with generic type parameters. This results in type checking and these conditions ensure that variables have a certain type of val 2 min read TypeScript Working with Constrained Values TypeScript Working with Constrained Values specifies how can we deal with Constrained Values. Generic Constraints are used to specify limits to the types that can be used with generic type parameters. This results in type checking and these conditions ensure that variables have a certain type of val 2 min read TypeScript Generic Types TypeScript Generic Types can be used by programmers when they need to create reusable components because they are used to create components that work with various data types and this provides type safety. The reusable components can be classes, functions, and interfaces. TypeScript generics can be u 2 min read TypeScript Generic Types TypeScript Generic Types can be used by programmers when they need to create reusable components because they are used to create components that work with various data types and this provides type safety. The reusable components can be classes, functions, and interfaces. TypeScript generics can be u 2 min read TypeScript Using Class Types in Generics TypeScript Using class types in generics allows you to create more flexible and reusable code by specifying that a generic parameter should be a class constructor. This is particularly useful when you want to work with instances of classes but want to keep the code type safe. You can define a generi 3 min read TypeScript Using Class Types in Generics TypeScript Using class types in generics allows you to create more flexible and reusable code by specifying that a generic parameter should be a class constructor. This is particularly useful when you want to work with instances of classes but want to keep the code type safe. You can define a generi 3 min read Like