Want a faster signal before interviews? Take the Interview Ready Check →

DAY 3 OF 30

Arrays and tuples

Yesterday, in one sentence

What is type inference in TypeScript?

Interview question

What is the difference between a type annotation and type inference?

You add a type annotationType Annotation to an array by writing a type followed by []. This is called an array type, and it says that every value inside the array is of that type.

let scores: number[] = [90, 85, 70];

scores.push(65); // ok
scores.push("high"); // ❌ Type 'string' is not assignable to type 'number'

A tuple is an array with a fixed length, where each position has its own type. You write the types as a list inside square brackets.

let pair: [string, number] = ["age", 41];

pair[0]; // string
pair[1]; // number
pair = [41, "age"]; // ❌ the types are in the wrong positions

Check yourself

Which of these is a tuple type?
What is the difference between an array type and a tuple type in TypeScript?