October 2017
Intermediate to advanced
280 pages
6h 50m
English
In TypeScript, arrays are JavaScript arrays with a common element type. This means that we cannot have elements from different types in a given array. We have different array types for all the built-in types in TypeScript, plus all the custom types that we define.
We can define an array of numbers as follows:
let primes: number[] = []; primes.push(2); primes.push(3);
If we want to have an array, which seems heterogeneous, similar to the arrays in JavaScript, we can use the any[] type:
let randomItems: any[] = [];
randomItems.push(1);
randomItems.push('foo');
randomItems.push([]);
randomItems.push({});
This is possible since the types of all the values we're pushing to the array are subtypes of the any type and the array ...
Read now
Unlock full access