Getting Started

Basic Types

Explore TypeScript primitive types, arrays, tuples, enums, and special types like any and unknown.

Primitive Types

TypeScript includes all JavaScript primitives plus additional annotations:

  • string — text values
  • number — integers and floats
  • boolean — true or false
  • null and undefined — absence of value
  • symbol — unique values
  • bigint — large integers

Special Types

  • any: Disables type checking — avoid when possible
  • unknown: Like any but forces you to check the type before using it
  • never: Represents values that never occur (e.g., functions that always throw)
  • void: Functions that don't return a value

Arrays and Tuples

Arrays hold multiple values of the same type. Tuples hold a fixed number of values with specific types at each position.

Example

typescript
// Primitives
const name: string = "TypeScript";
const version: number = 5.0;
const isAwesome: boolean = true;

// Arrays
const numbers: number[] = [1, 2, 3];
const names: Array<string> = ["Alice", "Bob"];

// Tuples (fixed-length, typed positions)
const point: [number, number] = [10, 20];
const entry: [string, number] = ["age", 30];

// Enums
enum Direction {
  Up = "UP",
  Down = "DOWN",
  Left = "LEFT",
  Right = "RIGHT",
}
const move: Direction = Direction.Up;

// Union types
let id: string | number;
id = "abc123";
id = 42;

// Literal types
type Status = "pending" | "active" | "inactive";
const userStatus: Status = "active";

// any vs unknown
let dangerous: any = "hello";
dangerous.toFixed(); // No error, but crashes at runtime

let safe: unknown = "hello";
if (typeof safe === "string") {
  safe.toUpperCase(); // OK, we checked the type
}

// Optional (undefined)
let middleName: string | undefined;
middleName = "Marie"; // OK
middleName = undefined; // Also OK
Try it yourself — TYPESCRIPT