- Published on
Arrays in typescript
- Authors
- Name
- Milad E. Fahmy
- @miladezzat12
Arrays
In JavaScript array can contain any type of data or mix of data types
Array Type Annotations
The TypeScript type annotation for array types is fairly straightforward: we put [] after the element type. In this code, names is an Array that can only contain strings:
let names: string[] = ['Danny', 'Samntha'];
// or you can declare as the following
let names: Array<string> = ['Danny','Samantha'];
if you try to assign number to names
array, you will get a type error
let names: string[] = [1,2,3] // Type Error!
names.push(562) // Type Error!
Multi-dimensional Arrays
You can declare multi demensional arrays as:
let myArray: string[][]