Code Reference

Annotating Basics

TypeScript · Reference cheat sheet

Annotating Basics

TypeScript · Reference cheat sheet


📋 Overview

Type annotations tell TypeScript what shape a value should have. You can annotate variables, parameters, and return types. Often TS infers types, so you only annotate where it helps clarity or catches mistakes.

🔧 Core concepts

PlaceExampleNotes
Variablelet n: number = 1Often inferred from the right-hand side
Parameterfunction f(x: string)Annotate public function inputs
Returnfunction f(): booleanOptional but useful for exports
Arraynumber[] or Array<number>Same idea
Object\{ id: number; name: string \}Or a type / interface alias
Unionstring | nullOne of several types

Start with primitives, arrays, and simple object types before generics.

💡 Examples

Variables and inference:

const count: number = 3;
const label = "docs"; // inferred as string
// label = 1; // Error
console.log(count, label);

Functions:

function clamp(n: number, min: number, max: number): number {
  return Math.min(max, Math.max(min, n));
}

console.log(clamp(15, 0, 10)); // 10

Objects and arrays:

type Point = { x: number; y: number };

const origin: Point = { x: 0, y: 0 };
const path: Point[] = [origin, { x: 1, y: 1 }];
console.log(path.length);

Union and optional:

function titleCase(text: string | null): string {
  if (text == null || text.length === 0) return "";
  return text[0].toUpperCase() + text.slice(1);
}

console.log(titleCase("ada"));
console.log(titleCase(null));

⚠️ Pitfalls

  • Redundant annotations (const x: number = 1) are fine while learning; later prefer inference.
  • any turns off checking — use unknown if you must accept “something.”
  • Optional (?) and | undefined interact with strictNullChecks.
  • Annotating the wrong type and forcing with as hides real bugs.

On this page