Sometimes, we want to declare a fixed length array in TypeScript.
In this article, we’ll look at how to declare a fixed length array in TypeScript.
How to declare a fixed length array in TypeScript?
To declare a fixed length array in TypeScript, we can use the Array
constructor or create a tuple.
For instance, we write
const arr = new Array<number>(3);
const tuple: [number, number, number] = [1, 2, 3];
to create the arr
array which is an array of numbers with new Array<number>(3)
.
And we create a tuple
with the [number, number, number]
tuple type which we set to an array with 3 numbers.
Conclusion
To declare a fixed length array in TypeScript, we can use the Array
constructor or create a tuple.