How to Build Multi Type Multidimensional Arrays in TypeScript

A multidimensional array sounds a lot more complicated than it really is. While a normal array consists of a list of numbers or other objects, a multidimensional array consists of a list of arrays. A more broad view is that an array is a list of values, sometimes primitives such as strings or numbers, but the values may also be other arrays. This composite data structure is how we are able to create matrices and model 2+ dimensional data.

The application of a multidimensional array can be as complex as modeling three dimensional objects or as simple as storing readable strings and values for populating a dropdown HTML element. This is straightforward in practice, but how do we implement this in TypeScript?

Simple Multidimensional TypeScript Arrays

One of the most basic multidimensional arrays we can create is one for storing matrices. Say we have the following structure we would like to display in TypeScript:

This is a three by three matrix which we would like to represent as a 3x3 array. Without TypeScript types we can express this data structure as the following:

Here we have three arrays with numbers nested inside a parent array. This gives us a multidimensional array of numbers. It works, but without typing it is not clear why this is structured this way, or even that it needs to be this way. To fix this we can give it a little TypeScript facelift:

We’re getting better as now it’s clear that our threeByThree variable needs to have number arrays nested in a parent array. However, we’re still missing a constraint to ensure that each child array has three number elements. We can fix that as the following:

Now there’s no getting around how our threeByThree variable can be built. It has to be a two dimensional array of arrays where each array has three number values. And if a user comes and adds a fourth element to one of the arrays:

We get this error enforcing that the array must have the specified number of elements and they all must be numbers!

Multi Type Multidimensional TypeScript Arrays

It’s not too hard to build multidimensional arrays when all the elements are of the same type. But how do we go about building multidimensional arrays when we have mixed types for our array elements?

Before we dive into this, what kind of applications could we use this for? A common one is to create a set of [“readableString”, valueId] pairs for building dropdown menus. Another use I have found is building string-number sets that allow you to filter the strings you include in your collection based on if the number is within a range. For our example let’s use the dropdown scenario:

Above we see we have cartItems, which is an array of arrays where we display item colors and their ids. We can feed this into a map function to output a set of HTML option tags, but only if the array is formatted correctly. To ensure correct formatting let’s add some TypeScript!

To do this we will again give cartItems a type of a double array, but this time we can’t specify just a number as we have both string and number values in our arrays. In order to represent these more complex members we will do the following:

Just like when we specified that we would have three numbers per array, we can specify our complex member arrays by placing string and number within the inner array type declaration. Here TypeScript will know to check for a string and number value before allowing the variable to be valid. We’re all set!

Just a quick heads up for typing with arrays. Types put outside the array brackets serve as count wildcards and allow any number of elements within them. Types put inside the brackets will enforce the quantity of that type declaration that are placed within the array.

Conclusion

The applications for multidimensional arrays is endless. Because the combination of structures that compose multidimensional arrays can be complex, it is important to define acceptable values using TypeScript. I hope that this article was helpful in your understanding of both simple and more complex multidimensional array typing and definitions!

Notes

https://stackoverflow.com/questions/30144580/typescript-multidimensional-array-initialization

https://www.tutorialspoint.com/typescript/typescript_multi_dimensional_arrays.htm

https://dpericich.medium.com/how-to-use-the-union-operator-to-create-mixed-type-arrays-in-typescript-1c208b48c139

https://www.codecademy.com/learn/learn-typescript/modules/learn-typescript-complex-types/cheatsheet

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Daniel Pericich

Daniel Pericich

Former Big Beer Engineer turned Full Stack Software Engineer