Programming Language
Updated for 2026

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.

Target Version Compatibility

Interactive Skill Mastery

Mark commands as learned to build your customized reference tracker. Retained locally in this browser.

Level:Novice
Command Mastery Progress0 of 24 Mastered (0%)

Basic Types

let isDone: boolean = false; let age: number = 42; let name: string = "Jane";
BeginnerBasics
Explicitly declare variables with primitive static 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

Console / Terminal
TS compilation succeeded with 0 static type errors.
let list: number[] = [1, 2, 3]; let tuple: [string, number] = ["hello", 10];
BeginnerBasics
Declare typed arrays and fixed-length, strictly ordered tuple structures.

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

Console / Terminal
TS compilation succeeded with 0 static type errors.

Interfaces & Types

interface User { id: number; name: string; email?: string; }
BeginnerBasics
Establish object contract contracts with mandatory and optional properties.

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

Console / Terminal
TS compilation succeeded with 0 static type errors.
type Point = { x: number; y: number; }; type ID = string | number;
BeginnerBasics
Define flexible type aliases supporting primitive combinations, unions, or object structures.

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

Console / Terminal
TS compilation succeeded with 0 static type errors.
type ResponseState = | { status: "success"; data: string } | { status: "error"; error: Error };
IntermediateDebugging
Define a discriminated union type to model mutual exclusion state shapes safely.

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

Console / Terminal
TS compilation succeeded with 0 static type errors.
interface Employee extends User { department: string; }
BeginnerBasics
Extend an existing interface to inherit attributes and define more specialized schemas.

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

Console / Terminal
TS compilation succeeded with 0 static type errors.
type EventName = `on${Capitalize<string>}`;
BeginnerBasics
Construct template literal types to build formatted string union sets dynamically at compile-time.

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

Console / Terminal
TS compilation succeeded with 0 static type errors.
abstract class Controller { abstract handleRequest(): void; public log() { console.log("Log"); } }
IntermediateDebugging
Declare abstract classes with abstract method definitions to enforce standard structural constraints in children.

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

Console / Terminal
TS compilation succeeded with 0 static type errors.

Generics

function identity<T>(arg: T): T { return arg; }
BeginnerBasics
Implement reusable generic functions that adapt dynamically to any argument type.

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

Console / Terminal
TS compilation succeeded with 0 static type errors.

Utility Types

type Preview = Pick<User, "id" | "name">;
BeginnerBasics
Utility Type: Construct a custom type picking specific properties from an existing interface.

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

Console / Terminal
TS compilation succeeded with 0 static type errors.
type ReadonlyUser = Readonly<User>;
BeginnerBasics
Utility Type: Convert all properties of an interface into immutable, readonly attributes.

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

Console / Terminal
TS compilation succeeded with 0 static type errors.
type PartialUser = Partial<User>;
BeginnerBasics
Utility Type: Make all properties of an interface optional dynamically.

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

Console / Terminal
TS compilation succeeded with 0 static type errors.
type RoleMap = Record<string, boolean>;
BeginnerBasics
Utility Type: Map dictionary structures with typed string keys and boolean values easily.

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

Console / Terminal
TS compilation succeeded with 0 static type errors.
type SafeUser = Omit<User, "password" | "salt">;
BeginnerBasics
Utility Type: Exclude specific sensitive fields from an existing type definition to generate clean schemas.

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

Console / Terminal
TS compilation succeeded with 0 static type errors.
type CustomGetter<T> = { [K in keyof T as `get${Capitalize<K & string>}`]: () => T[K] };
BeginnerBasics
Create a mapped type with key remapping to generate specialized getters for any object interface structure.

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

Console / Terminal
TS compilation succeeded with 0 static type errors.
type TaskCallback = (...args: any[]) => void; type TaskReturn = ReturnType<TaskCallback>; type TaskParams = Parameters<TaskCallback>;
BeginnerBasics
Infer the precise return type and function parameter types from any callable function signature.

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

Console / Terminal
TS compilation succeeded with 0 static type errors.
type DeepPartial<T> = { [P in keyof T]?: T[P] extends object ? DeepPartial<T[P]> : T[P]; };
BeginnerBasics
Create an advanced mapped type that makes all nested object properties optional recursively.

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

Console / Terminal
TS compilation succeeded with 0 static type errors.

Advanced Assertions

const colors = ["red", "green"] as const;
AdvancedPerformance
Apply deep readonly assertion to arrays or object literal definitions.

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

Console / Terminal
TS compilation succeeded with 0 static type errors.
function isString(x: unknown): x is string { return typeof x === "string"; }
AdvancedPerformance
Define a custom type guard predicate function to narrow down unknown variable 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

function isString(x: unknown): x is string {
  return typeof x === "string";
}

Output Example

Console / Terminal
TS compilation succeeded with 0 static type errors.
type UserKeys = keyof User;
AdvancedPerformance
Extract all declared keys of a type/interface as a union of literal string constants.

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

Console / Terminal
TS compilation succeeded with 0 static type errors.
type IsString<T> = T extends string ? true : false;
AdvancedPerformance
Define an advanced conditional type to branch type resolutions dynamically based on type inheritance matches.

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

Console / Terminal
TS compilation succeeded with 0 static type errors.
const config = { host: "localhost" } satisfies Record<string, string>;
AdvancedPerformance
Use the 'satisfies' operator to validate expression values against shapes without widening the resolved type.

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

Console / Terminal
TS compilation succeeded with 0 static type errors.
function assert(condition: any, message: string): asserts condition { if (!condition) throw new Error(message); }
AdvancedPerformance
Implement assertion functions that narrow types downstream by throwing exceptions on invalid runtime conditions.

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

Console / Terminal
TS compilation succeeded with 0 static type errors.
type Nominal<T, Brand> = T & { readonly __brand: Brand }; type Email = Nominal<string, "Email">;
AdvancedPerformance
Create branded nominal types to distinguish semantically separate primitive values at compile time.

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

Console / Terminal
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

Error

Type 'string' is not assignable to type 'number'

Solution

Correct the variable assignments or adjust the interfaces to match the assigned data payload type safely.

Error

Property 'x' does not exist on type 'y'

Solution

The object properties do not match the expected type schema. Declare optional keys (x?: string) or apply narrowing checks.

Error

Object is possibly 'undefined' or 'null'

Solution

Use optional chaining (user?.name) or write type narrowing conditions: if (user) { console.log(user.name); } to satisfy safety checks.

Error

Cannot find name 'myVariable'

Solution

Ensure variables are correctly imported, spelt correctly, or declared before execution inside the active scope.

Error

Overriding static signatures incorrectly

Solution

Verify that children inherit signatures cleanly and don't change parameters without satisfying polymorphic typing standards.

Error

Excess Property Checking warning on literal assignments

Solution

TypeScript flags unlisted properties when passing an object literal directly. Fix by assigning the object to an intermediate variable first, or defining index signatures.

Error

Index signature is missing in type 'X'

Solution

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.