On this page

Primitive Types

Enrico Hochkoeppler Profile

By Enrico Hochkoeppler

Web Developer & TutoriaLibre Founder

TypeScript builds on JavaScript’s primitive values, but adds static type checking that helps you catch mistakes before your code runs. Understanding how TypeScript models these primitives is the foundation for everything else in the type system.


The Core Primitive Types​

string​

Represents textual data.


let username: string = "Alice";

TypeScript ensures you don’t accidentally assign non‑string values:


username = 42; // ❌ Error

number​

Represents all numeric values (integers, floats, NaN, Infinity).


let price: number = 19.99;

TypeScript prevents accidental string concatenation:


price = "19.99"; // ❌ Error

boolean​

Represents true or false.


let isLoggedIn: boolean = false;

null and undefined​

These represent the absence of a value.

In TypeScript’s strict mode, they are not assignable to other types:


let count: number = null; // ❌ Error (in strict mode)

let name: string = undefined; // ❌ Error

This prevents a huge class of runtime crashes.

If you want to allow them, you must explicitly include them:


let maybeName: string | null = null;

The “Escape Hatches”: any and unknown​

JavaScript developers often reach for any because it feels familiar. But TypeScript gives you two very different tools for handling unknown values.


any — Opt Out of Type Safety​

any disables type checking entirely.


let data: any = fetchData();

data.toUpperCase(); // ✔ Allowed, even if data is not a string

Using any is basically telling TypeScript:

“Trust me, I know what I’m doing.”

This is dangerous because it reintroduces the exact runtime bugs TypeScript is designed to prevent.

Use any only when:


unknown — The Safe Alternative​

unknown means “this value could be anything,” but TypeScript forces you to check before using it.


let value: unknown = getValue();



value.toUpperCase();

// ❌ Error: Object is of type 'unknown'

You must narrow the type first:


if (typeof value === "string") {

  value.toUpperCase(); // ✔ Safe

}

This makes unknown the correct choice for:


When to Use unknown vs any​

ScenarioUse unknownUse anyYou want maximum safety✔You want to force type checks✔You’re handling external/unsafe data✔You’re migrating legacy JS✔You need a temporary workaround✔You want to disable type checking✔

Rule of thumb​

Use unknown unless you have a very specific reason to use any.

unknown preserves TypeScript’s guarantees.

any removes them.


Summary​

In this lesson, you learned how TypeScript models JavaScript’s primitive values:

Core primitives​

Special types​

Key takeaway​

unknown is the safe, modern alternative to any.

Use any sparingly and intentionally.

Found this helpful? Share the knowledge...

... or support this project to keep it alive!

Buy Me a Coffee

Previous

TypeScript's Type System

Next

Objects & Type Aliases