Skip to main content
TypeScript

TypeScript Best Practices for 2026

Modern TypeScript patterns every developer should know — strict mode, branded types, const assertions, discriminated unions, and more.

Author

Robert Baker

Published

Read time

2 min read

TypeScript has evolved rapidly. Patterns that were cutting-edge in 2023 are now table stakes. Here’s what modern TypeScript looks like in 2026.

Always Use Strict Mode

If your tsconfig.json doesn’t include "strict": true, you’re leaving type safety on the table. Strict mode enables:

  • strictNullChecks — catches null/undefined bugs
  • noImplicitAny — forces explicit typing
  • strictFunctionTypes — correct function variance
  • exactOptionalPropertyTypes — distinguishes undefined from missing
{
  "compilerOptions": {
    "strict": true,
    "noUncheckedIndexedAccess": true,
    "exactOptionalPropertyTypes": true
  }
}

Branded Types for Domain Safety

Branded types prevent mixing up values that share the same primitive type:

type UserId = string & { readonly __brand: "UserId" };
type OrderId = string & { readonly __brand: "OrderId" };

function getOrder(orderId: OrderId) { /* ... */ }

const userId = "usr_123" as UserId;
const orderId = "ord_456" as OrderId;

getOrder(userId);  // Type error — can't pass UserId as OrderId
getOrder(orderId); // Works

This catches an entire class of bugs at compile time with zero runtime cost.

Discriminated Unions Over Enums

Prefer discriminated unions for state modeling. They’re more powerful than enums and work naturally with narrowing:

type ApiResult<T> =
  | { status: "loading" }
  | { status: "error"; error: Error }
  | { status: "success"; data: T };

function handleResult(result: ApiResult<User>) {
  switch (result.status) {
    case "loading":
      return <Spinner />;
    case "error":
      return <ErrorMessage error={result.error} />;
    case "success":
      return <UserCard user={result.data} />;
    // TypeScript ensures exhaustive handling
  }
}

satisfies for Type Validation

The satisfies operator validates a value against a type without widening it:

const routes = {
  home: "/",
  about: "/about",
  blog: "/blog",
} satisfies Record<string, string>;

// routes.home is typed as "/" (literal), not string

Use as const for Immutable Data

const HTTP_METHODS = ["GET", "POST", "PUT", "DELETE"] as const;
type HttpMethod = (typeof HTTP_METHODS)[number];
// HttpMethod = "GET" | "POST" | "PUT" | "DELETE"

Prefer unknown Over any

When you genuinely don’t know a type, use unknown instead of any. It forces you to narrow before use:

function processInput(input: unknown) {
  if (typeof input === "string") {
    return input.toUpperCase(); // Safe — narrowed to string
  }
  if (input instanceof Error) {
    return input.message; // Safe — narrowed to Error
  }
  throw new Error("Unexpected input type");
}

Template Literal Types

Build complex string types from simpler ones:

type HttpMethod = "GET" | "POST" | "PUT" | "DELETE";
type ApiRoute = `/api/${string}`;
type Endpoint = `${HttpMethod} ${ApiRoute}`;
// "GET /api/users" | "POST /api/users" | ...

Key Takeaways

  1. Enable strict mode and noUncheckedIndexedAccess in every project
  2. Use branded types for domain identifiers
  3. Model state with discriminated unions, not enums
  4. Prefer satisfies over type annotations when you want literal types preserved
  5. Ban any from your codebase — use unknown and narrow

TypeScript’s type system is one of the most powerful in mainstream programming. These patterns help you use it to its full potential.

Need help modernizing your TypeScript codebase? →

Topics covered TypeScriptBest PracticesJavaScriptDeveloper Tools
Need dev help?

Get expert development help fast

Our engineering team turns complex ideas into production-ready software tailored to your business.

Book a consult
Next up

Continue leveling up your engineering skills

Dive deeper with related guides chosen to complement this topic and accelerate your next project.

Stay connected

Get engineering insights every week

Subscribe for framework updates, architecture patterns, and deep dives tailored to busy engineering teams.

Subscribe to Our Newsletter

Get tech tips, special offers, and updates delivered to your inbox.