Primitive Types
_TypeScript · Reference cheat sheet_
Primitive Types
TypeScript · Reference cheat sheet
📖 Overview
Primitives are the built-in scalar types: string, number, bigint, boolean, symbol, null, and undefined. TypeScript mirrors JavaScript’s runtime primitives and adds compile-time distinctions (e.g. literal types, strictNullChecks).
🧩 Core concepts
string/number/boolean— everyday scalars;numbercovers IEEE-754 floats (no separateint).bigint— arbitrary-precision integers (100n); not assignable to/fromnumberwithout conversion.symbol— unique keys;unique symbolfor singleton symbol types.null/undefined— withstrictNullChecks, not assignable to other types unless unioned.void— function return with no useful value (usuallyundefinedat runtime).- Boxed wrappers — avoid
String,Number,Booleanobject types; use primitives.
💡 Examples
const name: string = "Ada";
const age: number = 36;
const ok: boolean = true;
const huge: bigint = 10n ** 20n;
const id: symbol = Symbol("id");
function log(msg: string): void {
console.log(msg);
}
// strictNullChecks
let maybe: string | null = null;
maybe = "ready";
// Template / string primitives
const greeting: string = `Hello, ${name}`;
// Prefer primitives over wrappers
const bad: String = new String("no"); // avoid
const good: string = "yes";⚠️ Pitfalls
typeof null === "object"in JS — use=== nullchecks, nottypeof.NaNis anumber; useNumber.isNaNfor checks.- Without
strictNullChecks,null/undefinedsneak into almost every type. voidis not the same asundefinedin all assignability positions (callback returns).