TypeScript Cheatsheet - Type Types & Generics Reference

All the TypeScript types and syntax you need for daily development — from basic types to advanced generics, organized by category, ready to copy into your code.

Languages·23 commands·Last updated 2026-07-21
Back to Languages

Basic Types 8

let name: string = "John"
String type
let age: number = 30
Number type
let isActive: boolean = true
Boolean type
let items: string[] = ["a", "b"]
Array type
let tuple: [string, number] = ["a", 1]
Tuple type
let value: any = "anything"
Any type (use with caution)
let value: unknown = "safe"
Unknown type (safe)
let value: never
Never type

Interfaces & Types 5

interface User { name: string; age: number }
Define an interface
type User = { name: string; age: number }
Type alias
interface Admin extends User { role: string }
Interface inheritance
type Status = "active" | "inactive"
Union type
type Result = string | null | undefined
Nullable type

Generics 4

function identity<T>(arg: T): T { return arg }
Generic function
interface Container<T> { value: T }
Generic interface
type Pair<T, U> = [T, U]
Multi-parameter generics
function getFirst<T>(arr: T[]): T
Generic constraint

Utility Types 6

Partial<User>
All properties optional
Required<User>
All properties required
Readonly<User>
All properties readonly
Pick<User, "name" | "age">
Pick a subset of properties
Omit<User, "age">
Omit specific properties
Record<string, number>
Key-value pair type

💡 Tips

  • unknown is safer than any — it requires type checking or type assertion before use.
  • interface supports declaration merging, type does not; type supports unions and mapped types.
  • as const makes object properties literal types, e.g., { role: "admin" as const }.
  • The satisfies operator (TS 4.9+) validates types while preserving the inferred type.