Sometimes, we want to declare an array in TypeScript.
In this article, we’ll look at how to declare an array in TypeScript.
How to declare an array in TypeScript?
To declare an array in TypeScript, we can declare them in several different ways.
For instance, we write
const arr1: boolean[] = [];
const arr2: Array<boolean> = [];
const arr3 = [] as boolean[];
const arr4 = <boolean[]>[];
const arr5 = <Array<boolean>>new Array();
to declare a boolean array in 5 different ways.
We can set the variable type to boolean[]
or Array<boolean>
.
And we can cast the type of the variable to a boolean array with as boolean[]
, <boolean[]>
, or <Array<boolean>>
.
Conclusion
To declare an array in TypeScript, we can declare them in several different ways.