Jamie Balfour

Welcome to my personal website.

Find out more about me, my personal projects, reviews, courses and much more here.

Part 3.1Type annotations and inference

A type annotation follows a variable name with a colon. Common primitive types are string, number and boolean.

Annotations

TypeScript
let score: number = 0;
let playerName: string = "Ada";
let isComplete: boolean = false;

Inference

You do not need an annotation when TypeScript can clearly infer one from the initial value. In the following code, score is inferred as a number. TypeScript will reject assigning a string later.

TypeScript
let score = 0;
score += 10;

Avoiding any

The any type turns off checking for a value. It can be useful temporarily at an untyped boundary, but it spreads uncertainty through a program. Prefer unknown when a value is genuinely not known: it forces you to check the value before using it.

Feedback 👍
Comments are sent via email to me.