Course contentsShow
← TypeScript
Lesson 97 of 1,7963. Basic TypesFree lesson

Assignability Between Primitives

Learn which primitive types can be assigned to each other and which cannot.

Assignability Between Primitives

What you'll learn: You'll discover which primitive types TypeScript allows you to assign to each other—and which combinations it rejects.

The Core Idea

In TypeScript, assignability means "can I put value A into a location expecting type B?" For primitives, the rule is simple but strict: you can only assign a value to a variable if the types match exactly (or if the value is a literal that fits the type).

TypeScript won't let you mix primitive types freely, even when JavaScript would allow it at runtime. This prevents accidental type confusion.

What Works and What Doesn't

These assignments work:

let myNumber: number = 42;
let anotherNumber: number = myNumber; // ✓ number → number

let myString: string = "hello";
let anotherString: string = myString; // ✓ string → string

These assignments fail:

let myNumber: number = 42;
let myString: string = myNumber; // ✗ number → string BLOCKED

let myBool: boolean = true;
let myNum: number = myBool; // ✗ boolean → number BLOCKED

Even though JavaScript would coerce these values at runtime (like true becoming 1), TypeScript stops you. This prevents bugs where you accidentally treat a number as a string or vice versa.

Special Cases

null and undefined have their own assignability rules (more on that later with strict mode). For now, remember: primitives stay in their lanes.

Think of it like labeled containers: a "numbers only" box won't accept strings, even if you could physically fit them inside. TypeScript enforces the labels.

Key Takeaway: TypeScript primitives are not interchangeable—number, string, boolean, bigint, and symbol cannot be assigned to each other, preventing accidental type mixing.