let x: number = 10 Number type
let s: string = "hello" String type
let b: boolean = true Boolean type
let arr: number[] = [1, 2, 3] Array type
let tuple: [string, number] Tuple type (fixed length & types)
let x: any Any type (opt out of type checking)
let x: unknown Unknown type (safer than any)
let x: never Never type (unreachable code)
let x: void Void type (no return value)
let x: null | undefined Null and undefined types
type ID = string | number Union type
type Point = { x: number; y: number } Type alias for object
const x = "hello" as const Const assertion (literal type)
interface User {
name: string;
age: number;
} Interface declaration
interface User {
email?: string;
} Optional property
interface User {
readonly id: number;
} Readonly property
interface Admin extends User {
role: string;
} Interface inheritance
interface Fn {
(x: number): string;
} Callable interface
interface Dict {
[key: string]: number;
} Index signature
interface A extends B, C {} Multiple inheritance
function identity<T>(arg: T): T {
return arg;
} Generic function
interface Box<T> {
value: T;
} Generic interface
class Container<T> {
constructor(public value: T) {}
} Generic class
function fn<T extends HasLength>(x: T) Generic constraint
function fn<T, K extends keyof T>(obj: T, key: K) keyof constraint
type Pair<T, U = T> = [T, U] Generic with default type
function fn<T extends string | number>(x: T) Union constraint
Partial<User> Make all properties optional
Required<User> Make all properties required
Readonly<User> Make all properties readonly
Pick<User, "name" | "age"> Pick subset of properties
Omit<User, "password"> Omit specific properties
Record<string, number> Object type with key/value types
ReturnType<typeof fn> Extract function return type
Parameters<typeof fn> Extract function parameter types
Awaited<Promise<string>> Unwrap Promise type
NonNullable<string | null> Remove null/undefined from type
Extract<A | B | C, A | B> Extract matching union members
Exclude<A | B | C, A> Remove matching union members
if (typeof x === "string") typeof guard
if (x instanceof Date) instanceof guard
if ("name" in obj) in operator guard
function isUser(x: any): x is User {
return "name" in x;
} Custom type predicate
x as string Type assertion
x satisfies Type Satisfies operator (TS 4.9+)
enum Direction {
Up,
Down,
Left,
Right
} Numeric enum (0, 1, 2, 3)
enum Status {
Active = "ACTIVE",
Inactive = "INACTIVE"
} String enum
const enum Color { Red, Green, Blue } Const enum (inlined at compile)
type Keys = keyof User Get union of object keys
type Val = User["name"] Indexed access type
type Mapped = {
[K in keyof T]: boolean
} Mapped type
type IsString<T> = T extends string
? "yes" : "no" Conditional type
type Flatten<T> = T extends Array<infer U>
? U : T infer keyword
type Literal = "a" | "b" | "c" String literal union type
declare module "lib" {
export function fn(): void;
} Module declaration (ambient)
// @ts-ignore Suppress next-line TS error
// @ts-expect-error Expect error on next line
TypeScript Cheat Sheet — Interfaces, Generics, Utility Types & Type Guards
TypeScript launched in 2012 as a typed superset of JavaScript; the type system has grown a lot since — generics, conditional types, `infer`, template literal types, the `satisfies` operator — while the runtime stays plain JavaScript with no checks at all. The cheatsheet below covers 55+ type patterns across basics, interfaces, generics, utility types, type guards, enums, and advanced features. Most trouble in real TypeScript code doesn't come from forgetting syntax. It comes from quirks that look ordinary but bite. `any` and `unknown` look similar but behave opposite: `any` opts out of checking, while `unknown` requires a narrowing check before the value can be used. The `as` cast tells the compiler to trust the developer and silently disables the check at that line. Structural typing means two types with the same shape are interchangeable, which surprises authors coming from Java or C#. The snippets below are the ones an author reaches for when debugging exactly that — the right narrowing predicate, the right utility type, the right constraint — without rereading the TypeScript reference from line one each time.
Common pitfalls in TypeScript
A few patterns earn their place on the first screen of any TypeScript file. Reach for `unknown` over `any` whenever a value's shape is uncertain — `unknown` keeps the type system on and forces a check (`typeof`, `instanceof`, or a custom predicate) before any property access. Prefer the `satisfies` operator over a type annotation when assigning a constant: it validates the shape while keeping the literal type narrow. Strict null checks turn `null` and `undefined` into distinct types that have to be excluded with optional chaining, the `??` operator, or a type guard; without the flag both values slip into every type silently. Type assertions with `as` should be a last resort — a custom predicate `x is User` is verifiable, while `x as User` is a promise the compiler accepts without proof. Index signatures `[key: string]: T` accept reads of keys that may not exist, returning `T | undefined` only when `--noUncheckedIndexedAccess` is enabled. The cheatsheet groups this into Basic Types, Interfaces, Generics, Utility Types, Type Guards, Enums, and Advanced so the right section is one click away.
- 55+ TypeScript type patterns
- 7 categories from basics to advanced
- All utility types covered
- Generic patterns and constraints
- One-click copy to clipboard
- Covers TypeScript 5.x features
Free. No signup. Your inputs stay in your browser. Ads via Google AdSense (consent required).