M Logo
๐Ÿ“ TypeScript from Zero ยท Part 13 of 50

TypeScript Type Annotations vs Inference, Arrays and Tuples

November 6, 2026

Welcome to episode 13 of TypeScript from Zero, a series for complete beginners (no JavaScript needed). Welcome back to TypeScript from zero! Today: types that we write, and types that TypeScript figures out. We also meet two kinds of lists. We continue in the shop project, with a new file called annotations.ts. Every code block below is the code from the video, ready to copy; run each file to see the output.

Setup

We start in the terminal and open the project in VS Code:

bash
$ touch src/annotations.ts

1. Inference

We have already seen that TypeScript guesses types. We write let city equals Dhaka, with no type. TypeScript sees a piece of text, so city is a string. This guessing is called inference, and most of the time it is all we need.

ts
let city = "Dhaka";
console.log(city);
bash
$ node src/annotations.ts

2. Annotation

But we can also write the type ourselves, with a colon and the type name after the variable. Age colon number. That is called a type annotation. It helps when TypeScript cannot guess, and it tells the reader what we mean.

ts
let age: number = 30;
console.log(age);
bash
$ node src/annotations.ts

3. Array

An array type is the item type followed by square brackets. Scores colon number and brackets is a list of numbers. Only numbers are allowed inside.

ts
const scores: number[] = [90, 80, 70];
console.log(scores);
bash
$ node src/annotations.ts

4. Tuple

A tuple is a list with a fixed length, and a type for each position. Here the first item is a string, and the second is a number. Position zero is the name, position one is the age.

ts
const pair: [string, number] = ["Mia", 30];
console.log(pair[0], pair[1]);
bash
$ node src/annotations.ts

5. Wrong type

Now a mistake. The variable city got its type from the first value, a string. Later we give it the number forty two. Node does not mind, and prints forty two.

ts
city = 42;
console.log(city);
bash
$ node src/annotations.ts

6. tsc

tsc refuses: type number is not assignable to type string. The code is TS2322. A variable keeps its type for its whole life.

bash
$ npx tsc --noEmit

7. Fix

We delete those two lines, and tsc is quiet again.

bash
$ npx tsc --noEmit

Recap

Today: inference when TypeScript guesses, annotations when we write the type, array types, tuples, and the error for the wrong type. Next episode: more about typing functions.

ยฉ 2026 All Rights Reserved By Mahmudul Alam