Type Annotations for Primitives
In this lesson, you'll learn the exact syntax for explicitly telling TypeScript what type a variable should hold.
What Are Type Annotations?
You've already learned about TypeScript's primitive types (boolean, number, string, bigint, symbol, null, and undefined). Now it's time to learn how to explicitly declare which type a variable should be.
A type annotation is like a label you attach to a variable, telling TypeScript: "This box can only hold this specific kind of value." You write it using a colon (:) followed by the type name.
The Syntax
Here's the basic pattern:
let variableName: typeName = value;
For example:
let isDone: boolean = false;
let age: number = 25;
let firstName: string = "Alice";
let bigNumber: bigint = 100n;
let uniqueId: symbol = Symbol("id");
You can also declare a variable without immediately assigning a value:
let score: number;
score = 42; // Valid
Why Use Explicit Annotations?
TypeScript can often infer types automatically (you'll learn about this soon), but explicit annotations serve as documentation and guardrails. They make your intent crystal clear and help catch mistakes early.
Key Takeaway: Type annotations use a colon followed by the type name (e.g., let name: string = "value";) to explicitly declare what type a variable must hold, providing clarity and compile-time safety.