TypeScript Generic Types Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report 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 used in several ways like function, class, and interface generics. Syntax:function functionName<T> (returnValue : T) : T { return returnValue;}Example 1: In this example, we have created a generic function that can accept any type of data, we need to pass the value in the parameter by giving them any kind of data type and then we can reverse that value by the use of the reverseArray function. JavaScript function reverseArray<T>(array: T[]): T[] { return array.reverse(); } const strArray: string[] = reverseArray(["Java", "Python", "C++"]); const numArray: number[] = reverseArray([1, 2, 3, 4, 5]); const boolArray: boolean[] = reverseArray([false, true]); console.log(strArray); console.log(numArray); console.log(boolArray); Output: [ 'C++', 'Python', 'Java' ][ 5, 4, 3, 2, 1 ][ true, false ]Example 2: In this example, we have created a generic interface by using that we are creating object and string type of value and printing them in the console. JavaScript interface Resource<T> { id: number; resourceName: string; data: T; } const person: Resource<object> = { // Generic interface with objects id: 1, resourceName: 'Person', data: { name: 'John', age: 25, } } console.log(person); const employee: Resource<string[]> = { // Generic interface with strings array id: 2, resourceName: 'Employee', data: ['Employee 1', 'Employee 1'] } console.log(employee); Output: { "id": 1, "resourceName": "Person", "data": { "name": "John", "age": 25 }} { "id": 2, "resourceName": "Employee", "data": [ "Employee 1", "Employee 1" ]} Comment More infoAdvertise with us Next Article TypeScript Generic Types julietmaria Follow Improve Article Tags : JavaScript Web Technologies Geeks Premier League TypeScript Geeks Premier League 2023 +1 More Similar Reads Typescript Generic Type Array A generic type array is an array that is defined using the generic type in TypeScript. A generic type can be defined between the angled brackets(<>). Syntax:// Syntax for generic type arraylet myArray: Array<Type>;Example 1: Creating a simple generic type array of number type in Typescri 1 min read TypeScript Generic Object Types TypeScript Generic Object Types allow you to create flexible and reusable type definitions for objects. These generic types can work with different shapes of objects while providing type safety, ensuring your code is both robust and adaptable. They are particularly useful for creating functions or c 3 min read Higher-Order Types in TypeScript Higher-order types are among the advanced aspects of Typescript that give priority to types as first-class citizens, similar to higher-order functions of JavaScript that accept a function as an argument or return a function, higher-order types can accept types or return types.These are the following 6 min read TypeScript any Type In TypeScript, any type is a dynamic type that can represent values of any data type. It allows for flexible typing but sacrifices type safety, as it lacks compile-time type checking, making it less recommended in strongly typed TypeScript code. It allows developers to specify types for variables, f 4 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 Generics Interface in typescript "A major part of software engineering is building components that not only have well-defined and consistent APIs but are also reusable. " This sentence is in the official documentation we would start with. There are languages that are strong in static typing & others that are weak in dynamic typ 5 min read Data types in TypeScript In TypeScript, a data type defines the kind of values a variable can hold, ensuring type safety and enhancing code clarity.Primitive Types: Basic types like number, string, boolean, null, undefined, and symbol.Object Types: Complex structures including arrays, classes, interfaces, and functions.Prim 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 TypeScript Conditional Types In TypeScript, conditional types enable developers to create types that depend on a condition, allowing for more dynamic and flexible type definitions.They follow the syntax T extends U ? X : Y, meaning if type T is assignable to type U, the type resolves to X; otherwise, it resolves to Y.Conditiona 4 min read TypeScript Generic Classes Generics in TypeScript allow us to create reusable and type-safe components. Generic classes help in defining a blueprint that can work with different data types without sacrificing type safety. They enable better code reusability and flexibility by allowing us to define type parameters that will be 5 min read Like