Types
_TypeScript · Reference cheat sheet_
Types
TypeScript · Reference cheat sheet
📖 Overview
TypeScript adds a static type system on top of JavaScript. Types describe the shape of values so the compiler can catch mistakes before runtime. Prefer type aliases and interface for reusable shapes; use annotations where inference is unclear.
🧩 Core concepts
- Type annotations —
let x: number = 1declares the expected type. - Type inference — TS often infers types from initializers; annotate public APIs.
typevsinterface— both name object shapes;interfacesupports declaration merging;typecan express unions, intersections, and mapped forms.- Structural typing — compatibility is by shape, not by nominal class name.
any/unknown/never— escape hatch, safe top type, and bottom type respectively.- Type assertions —
as Tor<T>valuetell the compiler; they do not rewrite runtime values.
💡 Examples
// Annotation + inference
const count: number = 42;
const message = "hello"; // inferred as string
// type alias
type UserId = string;
type Point = { x: number; y: number };
// interface
interface User {
id: UserId;
name: string;
}
// Structural compatibility
const p: Point = { x: 1, y: 2 };
const alsoPoint = { x: 0, y: 0, z: 9 };
const ok: Point = alsoPoint; // extra props allowed when assigning from a variable
// unknown vs any
function parse(json: string): unknown {
return JSON.parse(json);
}
const data = parse("{}");
if (typeof data === "object" && data !== null && "id" in data) {
// narrowed
}
// Assertion (prefer narrowing when possible)
const el = document.querySelector("#app") as HTMLDivElement | null;⚠️ Pitfalls
- Overusing
anydisables checking; preferunknownand narrow. - Assertions lie to the compiler — invalid casts still compile.
- Fresh object literals are excess-property checked; variables are not.
interfacemerging can surprise you across files; keep declarations intentional.