Skip to content
AgileLogics

TypeScript Cheat Sheet

Learn TypeScript from the basics →

Basic Types

let x: string = "hi";
Explicit type annotation
let x: number, y: boolean
Number and boolean types
let arr: number[] = [1, 2];
Typed array
let t: [string, number]
Tuple (fixed-length, typed positions)

Objects & Interfaces

interface User { name: string; age?: number }
Interface with an optional property
type ID = string | number;
Type alias (union type)
const u: User = { name: "Ada" };
Use an interface as a type

Functions

function f(x: number): string { }
Typed parameter and return value
function f(x?: number) { }
Optional parameter
const f = (x: number): number => x * 2;
Typed arrow function

Generics

function first<T>(arr: T[]): T { return arr[0]; }
Generic function
class Box<T> { value: T; }
Generic class

Special Types

any
Opts out of type checking (avoid)
unknown
Safer "any" — must be narrowed before use
never
A value that never occurs (e.g. always throws)
as Type
Type assertion (tell the compiler what you know)

Utility Types

Partial<User>
All properties optional
Pick<User, "name">
Only the listed properties
Omit<User, "age">
All properties except the listed ones
Readonly<User>
All properties read-only