TypeScript - Basic Types

Primitive Types

  • string, number, boolean
  • null, undefined, void
  • any (escape hatch), unknown (safe top type), never (no values)
  • bigint, symbol

Examples

let s: string = "hello";
let n: number = 42;
let ok: boolean = true;
let id: symbol = Symbol("id");
let large: bigint = 9007199254740991n;

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

let anything: any = 123; // avoid in real code when possible
let top: unknown = JSON.parse("{}");

function fail(message: string): never { throw new Error(message); }

Literal and widening

const color = "red"; // type is "red" (literal)
let mutableColor = "red"; // type is string (widened)