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.
- All episodes are in the TypeScript from Zero playlist.
- Versions used: Node.js 26.13.0, npm 11.20.0, TypeScript 7.0.2.
Setup
We start in the terminal and open the project in VS Code:
$ touch src/annotations.ts1. 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.
let city = "Dhaka";
console.log(city);$ node src/annotations.ts2. 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.
let age: number = 30;
console.log(age);$ node src/annotations.ts3. 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.
const scores: number[] = [90, 80, 70];
console.log(scores);$ node src/annotations.ts4. 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.
const pair: [string, number] = ["Mia", 30];
console.log(pair[0], pair[1]);$ node src/annotations.ts5. 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.
city = 42;
console.log(city);$ node src/annotations.ts6. tsc
tsc refuses: type number is not assignable to type string. The code is TS2322. A variable keeps its type for its whole life.
$ npx tsc --noEmit7. Fix
We delete those two lines, and tsc is quiet again.
$ npx tsc --noEmitRecap
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.