Reference

Programming languages

Choose TypeScript, Go, Rust or C++ and find syntax, everyday APIs, standard libraries and tools in one place.

Scope and conventions

TypeScript/Node, Go 1.23+, Rust 2024 and C++23: from a first CLI to files, JSON, HTTP, concurrency and testing. Complete programs are labeled; other examples identify their surrounding code/imports. Optional libraries and version requirements are named in each entry.

Types & syntax

Read common type names

TypeScript

number is always an IEEE 754 double, so integers are doubles too (bigint holds arbitrary-size integers). string and boolean are primitive types. Type aliases/interfaces describe shapes; they do not construct runtime objects.

Fragment

Complete custom type and values.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
type Point = {
  x: number;
  y: number;
};

const point: Point = { x: 2, y: 5 };
const labels: string[] = ["north", "west"];

Decode punctuation

TypeScript

: annotates a type; . accesses a member. [] indexes or destructures. Backticks interpolate ${…}; \n, \r and \t escape LF, CR and tab. Single/double quotes both create strings.

Fragment

Fragment inside a function or module.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
const [first, second] = [2, 5];
const label = `item ${first}`;
const next = (n: number) => n + second;

Represent a missing value

TypeScript

With strictNullChecks, null and undefined must be allowed by the type. A ? property may be absent. ?. stops on null/undefined; ?? supplies a fallback for those two values, preserving false, 0 and empty text.

Fragment

Fragment: imports and type/function declarations at file scope; usage statements inside a function.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
type Profile = { nickname?: string };
const profile: Profile = {};

const label = profile.nickname?.toUpperCase() ?? "guest";

Define a record with named fields

TypeScript

type aliases and interfaces describe object shapes. readonly restricts writes through that type; it does not deeply freeze the object at runtime. An object literal supplies the actual value.

Fragment

Fragment: imports and type/function declarations at file scope; usage statements inside a function.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
interface Point {
  readonly x: number;
  y: number;
}

const point: Point = { x: 2, y: 5 };
point.y = 8;

Unpack multiple values

TypeScript

A tuple type records element positions and types. Destructuring binds positions to names. as const can infer a readonly literal tuple instead of a general mutable array.

Fragment

Fragment: imports and type/function declarations at file scope; usage statements inside a function.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
const item: [string, number] = ["north", 24];
const [name, count] = item;

Name alternatives and tagged variants

TypeScript

A literal union restricts allowed values. A discriminated union can associate different fields with each tag; checking the tag narrows the shape. A type alias is erased at runtime.

Fragment

Fragment: imports and type/function declarations at file scope; usage statements inside a function.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
type Mode = "read" | "write";
type Reply =
  | { kind: "ok"; value: number }
  | { kind: "failed"; reason: string };

const reply: Reply = { kind: "ok", value: 24 };

Read a type parameter

TypeScript

A generic parameter preserves a relationship between input and output types. Type arguments can often be inferred. An extends constraint limits which types are accepted; it is not runtime validation.

Fragment

Fragment: imports and type/function declarations at file scope; usage statements inside a function.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
function identity<T>(value: T): T {
  return value;
}

const count = identity<number>(24);
const name = identity("north");

Recognize a behavioral contract

TypeScript

Interfaces are structural: a value fits if it has the required members. implements checks a class declaration but does not create runtime dispatch or change method bodies.

Fragment

Fragment: imports and type/function declarations at file scope; usage statements inside a function.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
interface Named {
  name(): string;
}

const item: Named = {
  name() {
    return "north";
  }
};

Check a type or request a conversion

TypeScript

unknown requires narrowing before use; any disables many checks. typeof/instanceof/in are runtime checks that can narrow. as asserts a type to the checker; it does not validate or convert the runtime value.

Fragment

Fragment: imports and type/function declarations at file scope; usage statements inside a function.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
const value: unknown = "north";

if (typeof value === "string") {
  console.log(value.toUpperCase());
}

Read intersections and type operators

TypeScript

An intersection A & B requires both contracts. keyof obtains property keys; type-position typeof obtains the type of a value; T[K] selects a property’s type. These describe types, not runtime validation.

Fragment

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
type Named = { name: string };
type Counted = { count: number };
type Item = Named & Counted;
type Keys = keyof Item;
type Name = Item["name"];
const defaults = { retries: 3 };
type Defaults = typeof defaults;

Transform a type using mapped and utility types

TypeScript

Mapped types iterate keys to build another type. Partial, Pick, Omit, Record and ReturnType are compile-time utilities; none copies or validates runtime values.

Fragment

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
type Item = { id: number; name: string };
type Flags<T> = { [K in keyof T]: boolean };
type Patch = Partial<Item>;
type Label = Pick<Item, "name">;
type WithoutId = Omit<Item, "id">;
type Counts = Record<string, number>;
type Parsed = ReturnType<typeof JSON.parse>;

Keep literal information while checking a contract

TypeScript

as const narrows literals and adds readonly type information. satisfies checks compatibility while retaining the expression’s useful inferred type. Neither freezes an object or validates outside data at runtime.

Fragment

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
type Options = { mode: "fast" | "safe"; retries: number };
const options = {
  mode: "safe",
  retries: 3
} as const satisfies Options;