Sikau

TypeScript · Day 4 · about 20 min

Arrays and tuples

Lists of one type, and fixed-length pairs.

What's in the list?

An array of prices should only ever hold numbers. Tell TypeScript, and it guards every push, every map, every loop.

Array types and tuples

const prices: number[] = [180, 40, 90];
prices.push("free");

const names: Array<string> = ["Asha", "Ram"];
const upper = names.map((n) => n.toUpperCase());

const point: [number, number] = [27.7, 85.3];
const entry: [string, number] = ["momo", 180];
const [item, cost] = entry;

const days: readonly string[] = ["Sun", "Mon"];
days.push("Tue");

The rest of this lesson

  • Typed arrays in practiceexplain
  • Tuples and readonlyexplain
  • Is it allowed?predict
  • Type of findpredict
  • Your turnwrite code
  • Badge quizquiz
  • What you learnedrecap

Continue this lesson free — run real code in your browser

Continue with Google

See all 24 lessons of TypeScript