Typed Arrays
less than 1 minute read
Typed Arrays
Arrays in Typescript
const carMakers = ["ford", "toyota", "chevy"];
const carMakers = [] // => type: any[]
const carMakers: string[] = []
const carsByMake = [
['f150'],
['corolla'],
['camaro']
] // => type: string[][]

const carMakers: string[] = ["ford", "toyota", "chevy"];
// Help with inference when extracting values
const car = carMakers[0];
const myCar = carMakers.pop();
// Prevent incompatible values
carMakers.push(4); // => Make error
// Help with 'map'
carMakers.map((car): string => {
return car;
});
// Flexible Types
const importantDates: (Date | string)[] = [new Date(), "2020-01-01"];
importantDates.push("2030-10-10");
importantDates.push(new Date());
