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"
]
}
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