Coding for All
Lesson 3: Typed Arrays
Typed Arrays Arrays that only accept one type of item.
1const scores: number[] = [90, 85, 100];
2scores.push(95);
3console.log(scores);
Terminal
$ npx tsc main.ts
$ node main.js
[ 90, 85, 100, 95 ]
Every line, explained
const scores: number[] = [90, 85, 100];
number[] means "an array of numbers": the type followed by square brackets. Only numbers may go in: scores.push("ninety") would be an error. The mistake would be caught while compiling, BEFORE the program ever runs. That is TypeScript's whole superpower. (Secret: TypeScript could work this type out by itself just by looking at the values; writing the annotation is you being explicit.)
scores.push(95);
95 is a number, so TypeScript allows it. The array grows to four items; remember from the JavaScript course that const only locks the name, not the contents.
console.log(scores);
console.log(...) works exactly as it does in JavaScript; TypeScript IS JavaScript underneath, so everything from Intro to JavaScript still applies.