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

Common Primitive Pitfalls

Avoid mistakes like using Number instead of number or forgetting strictNullChecks behavior.

Common Primitive Pitfalls

You'll learn: The most frequent mistakes beginners make when working with TypeScript's primitive types — and how to avoid them.

The Two Cardinal Sins

Even after understanding primitives, beginners often stumble into avoidable traps. The most common mistakes revolve around wrapper types and null/undefined handling.

Pitfall #1: Using Wrapper Types (Number, String, Boolean)

Remember the difference between number and Number? TypeScript expects the lowercase primitive types. Using the capitalized versions (object wrappers) creates subtle bugs:

// ❌ WRONG: Using wrapper type
let count: Number = 5;

// ✅ CORRECT: Using primitive type
let count: number = 5;

The wrapper types (Number, String, Boolean) are almost never what you want. They represent the object form, not the actual primitive value. Think of it like ordering "water" but accidentally getting "bottled water container" instead of the water itself.

Pitfall #2: Forgetting strictNullChecks Behavior

Without strictNullChecks enabled in your tsconfig.json, TypeScript allows null and undefined everywhere — hiding potential runtime errors:

// With strictNullChecks: false (unsafe!)
let name: string = null; // No error, but dangerous!

// With strictNullChecks: true (safe!)
let name: string = null; // ❌ Error: Type 'null' is not assignable
let name: string | null = null; // ✅ Explicitly allowed

Always enable strictNullChecks (or use the strict flag) to catch these issues at compile-time rather than runtime.

Key Takeaway: Always use lowercase primitive types (number, string, boolean) not uppercase wrappers, and enable strictNullChecks to prevent null-related surprises.