Basic Types

beginner types primitives any unknown never

TypeScript ships with a small set of primitive types that cover almost everything we write. Let’s walk through them.

The familiar three

string, number, boolean — exactly what they sound like. No separate int or float in TS; everything is number (just like JS).

const name: string = "Manish";
const age: number = 27;       // ints and floats both
const isAdmin: boolean = true;

null and undefined

Two ways JS says “nothing here”. In simple language, undefined means “not assigned yet” and null means “explicitly empty”. With strictNullChecks on (which we always want), these are NOT assignable to other types.

let maybe: string | null = null;
maybe = "hello"; // fine
let nope: string = null; // ❌ error with strict mode

any — the escape hatch

any turns off type-checking for that value. It’s like writing plain JS. Useful while migrating a codebase, dangerous everywhere else.

let yolo: any = 5;
yolo.foo.bar.baz(); // no error — and a runtime explosion waiting to happen

Rule of thumb: if we’re reaching for any, we’re probably looking for unknown.

unknown — the safe any

unknown says “I don’t know what this is, and TS will force me to check before using it”. It’s the type-safe version of any.

function parse(input: unknown) {
  // input.toUpperCase(); // ❌ can't use it directly
  if (typeof input === "string") {
    return input.toUpperCase(); // ✅ narrowed to string
  }
}

void — function returns nothing

Used for functions that don’t return a value. Different from undefinedvoid says “the return value should be ignored”.

function logIt(msg: string): void {
  console.log(msg);
  // no return statement
}

never — this can never happen

never is the type of things that never produce a value. Functions that always throw, or infinite loops. It’s also what we get when a union is fully narrowed away.

function fail(msg: string): never {
  throw new Error(msg); // never returns
}

never shows up a lot in exhaustive switch checks — see the Discriminated Unions note.

Quick reference

const a: string = "x";
const b: number = 1;
const c: boolean = true;
const d: null = null;
const e: undefined = undefined;
const f: any = "anything";      // avoid
const g: unknown = "anything";  // prefer over any
function h(): void {}
function i(): never { throw new Error(); }

Interview soundbite

any skips the check, unknown forces a check. void is for functions returning nothing, never is for functions that can’t return at all (they throw or loop forever).”