Typescript Generic Type Array
Last Updated :
05 Feb, 2024
Improve
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 array
let myArray: Array<Type>;
Example 1: Creating a simple generic type array of number type in Typescript.
let numberArray: Array<number> =
[1, 2, 3, 4, 5];
console.log(numberArray);
Output:
[1, 2, 3, 4, 5]
Example 2: The below code creates a generic type array with which can contain values of different data types.
let numberArray: Array<number | string | boolean> =
[1, 2, "GeeksforGeeks", true, "JavaScript"];
console.log(numberArray);