TypeScript · Lesson 8 of 120
Arrays
Lists of one type.
An array is a list. In TypeScript, you say what kind of items it holds. A list of strings, a list of numbers. Then every item has to match.

- A list
- One kind of item
- Every item matches
Typed arrays
Stores is an array of strings, written string with square brackets. Prices is an array of numbers. Push adds a store. Reduce adds up the prices. Run it.
app.tstypescript
const stores: string[] = ["Leeds", "York"];
const prices: number[] = [849.5, 1299, 450];
stores.push("Hull");
const total = prices.reduce((sum, p) => sum + p, 0);
console.log(stores);
console.log("Total:", total);Output
[ 'Leeds', 'York', 'Hull' ] Total: 2598.5
Wrong item type
Now we try to push a number into the list of stores. Run it. Only strings are allowed.
app.tstypescript
const stores: string[] = ["Leeds", "York"];
stores.push(42);Output
app.ts line 2: Argument of type 'number' is not assignable to parameter of type 'string'.
Summary
Write the item type, then square brackets, and every item must match. Next, tuples.
Key takeaways- string[]
- number[]
- Items must match
Next up: Tuples — fixed positions