Primitive types

Topic 2 of 4 in Everyday types6 min readIntermediateFree course

Overview

Primitive types are the building blocks: text, numbers, true/false and the “empty” values. You already know them from JavaScript; this lesson is about how TypeScript treats them, and about the traps that types help you avoid.

The primitive types. string — “hello”, ‘hi’, Hi ${name} — text. number — 42, 3.14, -1, NaN, Infinity — all numbers, no separate int and float. boolean — true, false. bigint — 9007199254740993n — whole numbers of any size. symbol — Symbol(“id”) — unique keys. null — deliberately empty. undefined — not set yet. Use the lower-case names. String, Number and Boolean (capitalised) are object wrappers — avoid them as types.

string

TypeScript
const name: string = "Asha";
const greeting = `Hello, ${name}!`;   // template literals are strings too
name.toUpperCase();                   // the editor lists every string method
name.lenght;
// error TS2551: Property 'lenght' does not exist on type 'string'. Did you mean 'length'?

TypeScript knows every method of every built-in type, which is half the value of using it: misspelled methods are caught immediately, and autocomplete shows you what’s available.

number

JavaScript has one number type for integers and decimals alike, and so does TypeScript:

TypeScript
const count: number = 42;
const price = 3.99;
const big = 1_000_000;        // underscores for readability
const hex = 0xff;             // 255

Two special values are also numbers: NaN (“not a number”, the result of failed maths) and Infinity. TypeScript can’t tell them apart from normal numbers, so guard against them where they might appear:

TypeScript
const value = Number("12,50");      // NaN — the comma isn't valid
Number.isFinite(value);              // false: rejects NaN and ±Infinity

Decimal arithmetic is approximate (0.1 + 0.2 is 0.30000000000000004), exactly as in JavaScript. Types don’t change that — for money, work in whole cents or use a decimal library.

boolean

TypeScript
const isAdmin: boolean = false;
const canEdit = isAdmin || user.role === "editor";   // inferred boolean

bigint

number can only represent whole numbers exactly up to Number.MAX_SAFE_INTEGER (9,007,199,254,740,991). Beyond that, use bigint, written with an n suffix:

TypeScript
const id: bigint = 9007199254740993n;
const doubled = id * 2n;

TypeScript stops you from mixing the two, because JavaScript would throw at run time:

TypeScript
id * 2;
// error TS2365: Operator '*' cannot be applied to types 'bigint' and 'number'.

Convert explicitly with BigInt(2) or Number(id).

symbol

Symbols are unique values, mostly used as special object keys by libraries. You’ll rarely create your own:

TypeScript
const secret = Symbol("secret");   // unique symbol

null and undefined

  • undefined means “not set yet”: missing properties, unassigned variables, functions that return nothing.
  • null means “deliberately empty”.

With strict mode on (the default), they are separate types: a string can’t be null. If something can be missing, you say so with a union: string | null. That rule alone prevents the most common JavaScript crash, “Cannot read properties of undefined”. null and undefined is all about it.

No implicit conversions

JavaScript converts types silently; TypeScript flags most of these conversions instead:

TypeScript
const total = "10" * 2;
// error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.

const label = "Total: " + 20;   // allowed: concatenating a number into text is common and safe

When converting input, be explicit:

TypeScript
Number("42")        // 42
Number("4 2")       // NaN
parseInt("42px")    // 42 — stops at the first non-digit
String(42)          // "42"
(42).toFixed(2)     // "42.00"

Don’t use the wrapper types

JavaScript has object versions of primitives — String, Number, Boolean — and TypeScript has matching types. They are almost never what you want:

TypeScript
let a: String = "hi";   // works, but it's the wrong type
let b: string = "hi";   // ✓ always use the lower-case name

Two types for “nothing”: void and the empty values

A function that doesn’t return anything has the return type void:

TypeScript
function log(message: string): void {
  console.log(message);
}

You’ll meet void, unknown and never properly in any, unknown and never.

Remember: lower-case names, no silent conversions, bigint for huge integers, and Number.isFinite whenever a number came from user input.

Exercise

A typed toolbox

Write five small, fully annotated functions and call each one: (1) initials(fullName: string): string — "Ada King Lovelace" → "A.K.L."; (2) isAdult(age: number): boolean; (3) parsePrice(text: string): number that returns NaN-safe results — if Number(text) is not a finite number, return 0; (4) factorial(n: bigint): bigint — print factorial(25n), which is too big for a normal number; (5) formatBytes(bytes: number): string — 1536 → "1.5 KB", 5_000_000 → "4.8 MB", 512 → "512 B".

TypeScript
// 1. initials

// 2. isAdult

// 3. parsePrice

// 4. factorial with bigint

// 5. formatBytes

Summary

  • The primitives are string, number, boolean, bigint, symbol, null and undefined — always written in lower case.
  • number covers integers and decimals; NaN and Infinity are numbers too, so check with Number.isFinite.
  • bigint (written 10n) handles whole numbers of any size, but can’t be mixed with number without converting.
  • TypeScript never converts between types for you: "3" * 2 and "a" + 1 style code is flagged or must be explicit.
  • Avoid String, Number and Boolean (capitalised) as types — they are object wrappers.