How to Build Multi Type Multidimensional Arrays in TypeScript ?
Last Updated :
15 May, 2024
TypeScript, multi-type multidimensional Arrays allow us to create arrays that can hold elements of multiple types at each level of the array hierarchy. we will learn how to build multi-type multidimensional arrays.
These are the following methods:
Using Union Types
In this method, we use union types to define an array type that can hold elements of different types.
Syntax:
let multiArray: (string | number)[][]
Example: In this example, we create a Multi Type Multidimensional Arrays using union Types
JavaScript
type MultiTypeArray = (number | string | boolean)[][][];
const multiArray: MultiTypeArray = [
[[1, 'two', true], [false, 'five', 6]],
[['seven', 8, true], [false, 10, 'eleven']]
];
console.log(multiArray);
Output
[
[
[ 1, 'two', true ],
[ false, 'five', 6 ]
],
[
[ 'seven', 8, true ],
[ false, 10, 'eleven' ]
]
]
Using Tuple Types
In this method Tuple types are used to specify arrays with fixed lengths and element types, making them suitable for multidimensional arrays
Syntax:
let multiTypeArray: [string, number][]
Example: In this example, we create a Multi Type Multidimensional Arrays Using Tuple Types
JavaScript
type MultiTypeTuple = [number | string | boolean][][][];
const multiTuple: MultiTypeTuple = [
[[1, 'two', true], [false, 'five', 6]],
[['seven', 8, true], [false, 10, 'eleven']]
];
console.log(multiTuple);
Output
[
[
[ 1, 'two', true ],
[ false, 'five', 6 ]
],
[
[ 'seven', 8, true ],
[ false, 10, 'eleven' ]
]
]
Using Interface Types
In this method we define custom interfaces representing multidimensional arrays, specifying the types of elements at each level.
Syntax:
interface Person { name: type1; age: type2; }
let multiTypeArray: Person[]
Example: In this example, we create a Multi Type Multidimensional Arrays Using Interface Types
JavaScript
interface MultiTypeArray {
[index: number]: (number | string | boolean)[][];
}
const multiInterface: MultiTypeArray = [
[[1, 'two', true], [false, 'five', 6]],
[['seven', 8, true], [false, 10, 'eleven']]
];
console.log(multiInterface);
Output
[
[
[ 1, 'two', true ],
[ false, 'five', 6 ]
],
[
[ 'seven', 8, true ],
[ false, 10, 'eleven' ]
]
]
Using Generics
Generics provide another approach to building multi-type multidimensional arrays, offering flexibility and type safety.
Syntax:
class MultiTypeArray<T> {
private data: T[][][];
constructor(initialData: T[][][]) {
this.data = initialData;
}
getData(): T[][][] {
return this.data;
}
}
Example: The MultiTypeArray class defines a multi-dimensional array with elements of type number, string, or boolean. It initializes with given data and provides a method to access it.
JavaScript
class MultiTypeArray<T> {
private data: T[][][];
constructor(initialData: T[][][]) {
this.data = initialData;
}
getData(): T[][][] {
return this.data;
}
}
const multiGeneric = new MultiTypeArray<number | string | boolean>([
[[1, 'two', true], [false, 'five', 6]],
[['seven', 8, true], [false, 10, 'eleven']]
]);
console.log(multiGeneric.getData());
Output:
[[[1, "two", true], [false, "five", 6]], [["seven", 8, true], [false, 10, "eleven"]]]
Similar Reads
How To Get Types From Arrays in TypeScript? In TypeScript, arrays are a common data structure, but sometimes it's necessary to extract the types of elements stored in an array for type-checking or validation purposes. TypeScript provides several ways to extract types from arrays, enabling more type-safe operations.We will explore different me
3 min read
How to Define Type for Array With Unique Items in TypeScript ? Defining a type for an array with unique items in TypeScript ensures that your data structures are robust and error-resistant. Here, we'll explore several methods to implement this functionality, each tailored to different use cases.Below are the possible approaches:Table of Content1. Using Set2. Us
2 min read
Defining array with multiple types in TypeScript TypeScript is a statically typed language. So, we can create an array with explicit types to store entries of only specified types inside it. If an array is declared with only one explicit data type specified, then we can store the data of that particular data type inside that array. If you try to s
4 min read
How to Declare an Array of Strings in TypeScript ? Arrays are fundamental data structures in TypeScript, enabling developers to manage collections of elements efficiently. Below are the approaches to declare an Array of strings in TypeScript:Table of ContentSquare Brackets NotationArray ConstructorSquare Brackets NotationUsing square brackets notati
1 min read
How to Avoid Inferring Empty Array in TypeScript ? In TypeScript, the empty arrays are inferred as never[] by default, denoting an array incapable of containing any elements. This occurs when TypeScript fails to deduce the type of array elements from the context, notably in scenarios involving spread operators, the Array constructor, or methods like
2 min read
How to Iterate Array of Objects in TypeScript ? In TypeScript, we can iterate over the array of objects using various inbuilt loops and higher-order functions, We can use for...of Loop, forEach method, and map method. There are several approaches in TypeScript to iterate over the array of objects which are as follows:Table of ContentUsing for...o
3 min read