Ultimate TypeScript Cheatsheet & Modern Type Reference Guide
Master modern TypeScript 5.x with this interactive cheatsheet. From core primitives and interfaces to complex generics, utility types, discriminated unions, and advanced type-level programming patterns.
Interactive Skill Mastery
Mark commands as learned to build your customized reference tracker. Retained locally in this browser.
Basic Types
When to Use
When building scalable TypeScript applications requiring strict static compile-time type validation systems.
Common Mistakes
Overusing the loose 'any' type, which completely disables compiler type checks and defeats TypeScript's core benefits.
Shortcut / Pro-Tip
Always enable 'strict': true inside your project's tsconfig.json to enforce rigorous type checking rules.Example
let isDone: boolean = false;
let age: number = 42;
let name: string = "Jane";Output Example
TS compilation succeeded with 0 static type errors.When to Use
When building scalable TypeScript applications requiring strict static compile-time type validation systems.
Common Mistakes
Overusing the loose 'any' type, which completely disables compiler type checks and defeats TypeScript's core benefits.
Shortcut / Pro-Tip
Always enable 'strict': true inside your project's tsconfig.json to enforce rigorous type checking rules.Example
let list: number[] = [1, 2, 3];
let tuple: [string, number] = ["hello", 10];Output Example
TS compilation succeeded with 0 static type errors.Interfaces & Types
When to Use
When building scalable TypeScript applications requiring strict static compile-time type validation systems.
Common Mistakes
Overusing the loose 'any' type, which completely disables compiler type checks and defeats TypeScript's core benefits.
Shortcut / Pro-Tip
Always enable 'strict': true inside your project's tsconfig.json to enforce rigorous type checking rules.Example
interface User {
id: number;
name: string;
email?: string;
}Output Example
TS compilation succeeded with 0 static type errors.When to Use
When building scalable TypeScript applications requiring strict static compile-time type validation systems.
Common Mistakes
Overusing the loose 'any' type, which completely disables compiler type checks and defeats TypeScript's core benefits.
Shortcut / Pro-Tip
Always enable 'strict': true inside your project's tsconfig.json to enforce rigorous type checking rules.Example
type Point = { x: number; y: number; };
type ID = string | number;Output Example
TS compilation succeeded with 0 static type errors.When to Use
When building scalable TypeScript applications requiring strict static compile-time type validation systems.
Common Mistakes
Overusing the loose 'any' type, which completely disables compiler type checks and defeats TypeScript's core benefits.
Shortcut / Pro-Tip
Always enable 'strict': true inside your project's tsconfig.json to enforce rigorous type checking rules.Example
type ResponseState =
| { status: "success"; data: string }
| { status: "error"; error: Error };Output Example
TS compilation succeeded with 0 static type errors.When to Use
When building scalable TypeScript applications requiring strict static compile-time type validation systems.
Common Mistakes
Overusing the loose 'any' type, which completely disables compiler type checks and defeats TypeScript's core benefits.
Shortcut / Pro-Tip
Always enable 'strict': true inside your project's tsconfig.json to enforce rigorous type checking rules.Example
interface Employee extends User {
department: string;
}Output Example
TS compilation succeeded with 0 static type errors.When to Use
When building scalable TypeScript applications requiring strict static compile-time type validation systems.
Common Mistakes
Overusing the loose 'any' type, which completely disables compiler type checks and defeats TypeScript's core benefits.
Shortcut / Pro-Tip
Always enable 'strict': true inside your project's tsconfig.json to enforce rigorous type checking rules.Example
type EventName = `on${Capitalize<string>}`;Output Example
TS compilation succeeded with 0 static type errors.When to Use
When building scalable TypeScript applications requiring strict static compile-time type validation systems.
Common Mistakes
Overusing the loose 'any' type, which completely disables compiler type checks and defeats TypeScript's core benefits.
Shortcut / Pro-Tip
Always enable 'strict': true inside your project's tsconfig.json to enforce rigorous type checking rules.Example
abstract class Controller {
abstract handleRequest(): void;
public log() { console.log("Log"); }
}Output Example
TS compilation succeeded with 0 static type errors.Generics
When to Use
When building scalable TypeScript applications requiring strict static compile-time type validation systems.
Common Mistakes
Overusing the loose 'any' type, which completely disables compiler type checks and defeats TypeScript's core benefits.
Shortcut / Pro-Tip
Always enable 'strict': true inside your project's tsconfig.json to enforce rigorous type checking rules.Example
function identity<T>(arg: T): T {
return arg;
}Output Example
TS compilation succeeded with 0 static type errors.Utility Types
When to Use
When building scalable TypeScript applications requiring strict static compile-time type validation systems.
Common Mistakes
Overusing the loose 'any' type, which completely disables compiler type checks and defeats TypeScript's core benefits.
Shortcut / Pro-Tip
Always enable 'strict': true inside your project's tsconfig.json to enforce rigorous type checking rules.Example
type Preview = Pick<User, "id" | "name">;Output Example
TS compilation succeeded with 0 static type errors.When to Use
When building scalable TypeScript applications requiring strict static compile-time type validation systems.
Common Mistakes
Overusing the loose 'any' type, which completely disables compiler type checks and defeats TypeScript's core benefits.
Shortcut / Pro-Tip
Always enable 'strict': true inside your project's tsconfig.json to enforce rigorous type checking rules.Example
type ReadonlyUser = Readonly<User>;Output Example
TS compilation succeeded with 0 static type errors.When to Use
When building scalable TypeScript applications requiring strict static compile-time type validation systems.
Common Mistakes
Overusing the loose 'any' type, which completely disables compiler type checks and defeats TypeScript's core benefits.
Shortcut / Pro-Tip
Always enable 'strict': true inside your project's tsconfig.json to enforce rigorous type checking rules.Example
type PartialUser = Partial<User>;Output Example
TS compilation succeeded with 0 static type errors.When to Use
When building scalable TypeScript applications requiring strict static compile-time type validation systems.
Common Mistakes
Overusing the loose 'any' type, which completely disables compiler type checks and defeats TypeScript's core benefits.
Shortcut / Pro-Tip
Always enable 'strict': true inside your project's tsconfig.json to enforce rigorous type checking rules.Example
type RoleMap = Record<string, boolean>;Output Example
TS compilation succeeded with 0 static type errors.When to Use
When building scalable TypeScript applications requiring strict static compile-time type validation systems.
Common Mistakes
Overusing the loose 'any' type, which completely disables compiler type checks and defeats TypeScript's core benefits.
Shortcut / Pro-Tip
Always enable 'strict': true inside your project's tsconfig.json to enforce rigorous type checking rules.Example
type SafeUser = Omit<User, "password" | "salt">;Output Example
TS compilation succeeded with 0 static type errors.When to Use
When building scalable TypeScript applications requiring strict static compile-time type validation systems.
Common Mistakes
Overusing the loose 'any' type, which completely disables compiler type checks and defeats TypeScript's core benefits.
Shortcut / Pro-Tip
Always enable 'strict': true inside your project's tsconfig.json to enforce rigorous type checking rules.Example
type CustomGetter<T> = { [K in keyof T as `get${Capitalize<K & string>}`]: () => T[K] };Output Example
TS compilation succeeded with 0 static type errors.When to Use
When building scalable TypeScript applications requiring strict static compile-time type validation systems.
Common Mistakes
Overusing the loose 'any' type, which completely disables compiler type checks and defeats TypeScript's core benefits.
Shortcut / Pro-Tip
Always enable 'strict': true inside your project's tsconfig.json to enforce rigorous type checking rules.Example
type TaskCallback = (...args: any[]) => void;
type TaskReturn = ReturnType<TaskCallback>;
type TaskParams = Parameters<TaskCallback>;Output Example
TS compilation succeeded with 0 static type errors.When to Use
When building scalable TypeScript applications requiring strict static compile-time type validation systems.
Common Mistakes
Overusing the loose 'any' type, which completely disables compiler type checks and defeats TypeScript's core benefits.
Shortcut / Pro-Tip
Always enable 'strict': true inside your project's tsconfig.json to enforce rigorous type checking rules.Example
type DeepPartial<T> = {
[P in keyof T]?: T[P] extends object ? DeepPartial<T[P]> : T[P];
};Output Example
TS compilation succeeded with 0 static type errors.Advanced Assertions
When to Use
When building scalable TypeScript applications requiring strict static compile-time type validation systems.
Common Mistakes
Overusing the loose 'any' type, which completely disables compiler type checks and defeats TypeScript's core benefits.
Shortcut / Pro-Tip
Always enable 'strict': true inside your project's tsconfig.json to enforce rigorous type checking rules.Example
const colors = ["red", "green"] as const;Output Example
TS compilation succeeded with 0 static type errors.When to Use
When building scalable TypeScript applications requiring strict static compile-time type validation systems.
Common Mistakes
Overusing the loose 'any' type, which completely disables compiler type checks and defeats TypeScript's core benefits.
Shortcut / Pro-Tip
Always enable 'strict': true inside your project's tsconfig.json to enforce rigorous type checking rules.Example
function isString(x: unknown): x is string {
return typeof x === "string";
}Output Example
TS compilation succeeded with 0 static type errors.When to Use
When building scalable TypeScript applications requiring strict static compile-time type validation systems.
Common Mistakes
Overusing the loose 'any' type, which completely disables compiler type checks and defeats TypeScript's core benefits.
Shortcut / Pro-Tip
Always enable 'strict': true inside your project's tsconfig.json to enforce rigorous type checking rules.Example
type UserKeys = keyof User;Output Example
TS compilation succeeded with 0 static type errors.When to Use
When building scalable TypeScript applications requiring strict static compile-time type validation systems.
Common Mistakes
Overusing the loose 'any' type, which completely disables compiler type checks and defeats TypeScript's core benefits.
Shortcut / Pro-Tip
Always enable 'strict': true inside your project's tsconfig.json to enforce rigorous type checking rules.Example
type IsString<T> = T extends string ? true : false;Output Example
TS compilation succeeded with 0 static type errors.When to Use
When building scalable TypeScript applications requiring strict static compile-time type validation systems.
Common Mistakes
Overusing the loose 'any' type, which completely disables compiler type checks and defeats TypeScript's core benefits.
Shortcut / Pro-Tip
Always enable 'strict': true inside your project's tsconfig.json to enforce rigorous type checking rules.Example
const config = { host: "localhost" } satisfies Record<string, string>;Output Example
TS compilation succeeded with 0 static type errors.When to Use
When building scalable TypeScript applications requiring strict static compile-time type validation systems.
Common Mistakes
Overusing the loose 'any' type, which completely disables compiler type checks and defeats TypeScript's core benefits.
Shortcut / Pro-Tip
Always enable 'strict': true inside your project's tsconfig.json to enforce rigorous type checking rules.Example
function assert(condition: any, message: string): asserts condition {
if (!condition) throw new Error(message);
}Output Example
TS compilation succeeded with 0 static type errors.When to Use
When building scalable TypeScript applications requiring strict static compile-time type validation systems.
Common Mistakes
Overusing the loose 'any' type, which completely disables compiler type checks and defeats TypeScript's core benefits.
Shortcut / Pro-Tip
Always enable 'strict': true inside your project's tsconfig.json to enforce rigorous type checking rules.Example
type Nominal<T, Brand> = T & { readonly __brand: Brand };
type Email = Nominal<string, "Email">;Output Example
TS compilation succeeded with 0 static type errors.TypeScript Best Practices
1Enforce Strict Compiler Checks
Ensure 'strict': true is activated inside tsconfig.json to prevent silent type bypasses and catch errors early.
2Avoid the Loose 'any' Type
Overusing 'any' completely disables the compiler static type checker. Opt for 'unknown' if types are unpredictable, forcing runtime guards.
3Prefer Interfaces Over Types for Extensibility
Use interface declarations for defining API payloads and component contracts, as they support declaration merging and extends hierarchies.
4Adopt Discriminated Unions
Use literal properties (e.g. type: 'success' | 'error') to construct discriminated unions, making type-narrowing logical and safe.
5Leverage Standard Utility Types
Construct derived types elegantly using built-in utility classes like Partial, Readonly, Pick, and Omit rather than duplicating interfaces.
6Use the 'satisfies' Operator for Validation
Prefer satisfies over standard type annotations when you want to validate a value against a contract but preserve its precise literal type (e.g., preserving exact string values).
7Prefer Readonly Arrays & Properties
Enforce immutability using ReadonlyArray<T> or Readonly<T> to prevent accidental side effects and mutations in state managers.
8Avoid Numeric Enums, Prefer Const Objects
Numeric enums compile to bloated reverse-mapping JS objects. Use 'as const' on plain objects or union types for zero-cost runtime footprints.
Common TypeScript Errors & Solutions
Type 'string' is not assignable to type 'number'
Correct the variable assignments or adjust the interfaces to match the assigned data payload type safely.
Property 'x' does not exist on type 'y'
The object properties do not match the expected type schema. Declare optional keys (x?: string) or apply narrowing checks.
Object is possibly 'undefined' or 'null'
Use optional chaining (user?.name) or write type narrowing conditions: if (user) { console.log(user.name); } to satisfy safety checks.
Cannot find name 'myVariable'
Ensure variables are correctly imported, spelt correctly, or declared before execution inside the active scope.
Overriding static signatures incorrectly
Verify that children inherit signatures cleanly and don't change parameters without satisfying polymorphic typing standards.
Excess Property Checking warning on literal assignments
TypeScript flags unlisted properties when passing an object literal directly. Fix by assigning the object to an intermediate variable first, or defining index signatures.
Index signature is missing in type 'X'
Occurs when assigning an object to a map expecting string keys. Solve by declaring an index signature: [key: string]: any; or using Record<string, unknown>.
Common TypeScript Interview Questions
Q1What are the key benefits of using a TypeScript cheatsheet versus plain JavaScript cheat sheets?
TypeScript adds static type definitions, enabling compiler verification that catches code mistakes before production. It enhances developer productivity with robust autocompletion, refactoring safety, and self-documenting code contracts.
Q2What is the difference between Type Aliases and Interfaces in TypeScript?
Interfaces are extendable, support declaration merging, and are ideal for defining object shapes. Type aliases are more versatile and can declare union types, primitives, tuples, and intersections, but cannot be merged.
Q3Explain the 'unknown' type and how it differs from the 'any' type.
Both accept any value. However, 'any' completely bypasses type checks, allowing any operation. 'unknown' is type-safe: you cannot invoke methods or assign it to other types until you first verify or narrow down its type.
Q4How do Generics work in TypeScript and why are they used?
Generics allow developers to pass types as parameters to functions, interfaces, or classes, enabling type safety without sacrificing flexibility. They enable creating reusable, modular components that adapt to different type configurations.
Q5What is Discriminated Union and how do you implement it?
A Discriminated Union is a pattern where different types share a common literal tag property (the discriminant). By evaluating this tag (e.g., in a switch statement), TypeScript's control flow analysis narrows down the active type.
Q6How does the 'satisfies' operator differ from standard type annotations?
A type annotation (const x: T = ...) forces the value to match T and widens its type to T. The satisfies operator (const x = ... satisfies T) validates that the value fits T, but preserves the narrowest literal type of the expression, retaining specific keys and value constraints.
Q7What are mapped types and template literal types in TypeScript?
Mapped types allow you to declare new types by iterating over keys of an existing type (using syntax like [K in keyof T]). Template literal types let you construct complex string union sets dynamically using template-string syntax at compile-time (e.g., `on${Capitalize<string>}`).
Q8Explain TypeScript assertion functions and the 'asserts' keyword.
An assertion function is a utility that checks a condition and throws an error if it's invalid. By specifying the return type as 'asserts condition', you inform the compiler that if the function returns without throwing, the tested variable is guaranteed to have that asserted type downstream.
Related Resources
REST API Tester
Test API routes and endpoints directly in your browser with full request controls.
JSON Formatter & Validator
Beautify, validate, and minify JSON structures instantly.
Best Free Online Developer Tools
An expert review of must-have online utilities for developers.
Git Cheatsheet
Essential command reference for local and remote version control repositories.
Generated from LearnHubly Developer Cheatsheets
Access interactive sandbox tests, tools, and developer code bases at https://www.learnhubly.com