How to use Tuple Types In Typescript
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
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' ]
]
]
How to Build Multi Type Multidimensional Arrays in TypeScript ?
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:
Table of Content
- Using Union Types
- Using Tuple Types
- Using Interface Types
- Using Generics