Lesson

TypeScript Best Practices

10 min reading
Free Course

TypeScript Best Practices: Habits That Keep Codebases Clean

Good TypeScript makes bugs obvious and refactoring fearless without burying code under complex type gymnastics. Following a small set of team habits keeps compile times fast and types readable for every developer on the project.

The highest-impact step is turning on "strict": true and "noUncheckedIndexedAccess": true in tsconfig.json from day one.

Core Rules for Production TypeScript

  1. Ban implicit and lazy any: Use unknown for untrusted data (API JSON, catch errors) and narrow with type guards or schema validators like Zod.
  2. Use import type for type-only imports: Writing import type { User } from "./types" guarantees zero runtime bundle imports and prevents circular runtime dependency issues.
  3. Prefer as const unions over numeric enums: Literal unions ("idle" | "loading" | "error") are lighter, work with Node type stripping, and serialize cleanly.
  4. Derive DTOs with utility types: Build CreateUser = Omit<User, "id"> and UpdateUser = Partial<CreateUser> instead of copy-pasting properties across five files.
// Good: derive types from one source of truth + narrow unknown input
interface Order {
  readonly id: string;
  amount: number;
  status: "pending" | "paid";
}

type OrderDraft = Omit<Order, "id">;

function isOrderDraft(raw: unknown): raw is OrderDraft {
  return (
    typeof raw === "object" &&
    raw !== null &&
    typeof (raw as OrderDraft).amount === "number" &&
    ((raw as OrderDraft).status === "pending" || (raw as OrderDraft).status === "paid")
  );
}

const sample: unknown = { amount: 49.5, status: "paid" };
if (isOrderDraft(sample)) {
  console.log(sample.amount, sample.status);
}

Common Mistakes to Avoid

  • Silencing errors with // @ts-ignore or as any: Always prefer // @ts-expect-error (with a comment), which alerts you when the underlying error disappears.
  • Writing overly clever 10-line conditional types in app code: If a teammate cannot read a type signature in five seconds, simplify it with concrete interfaces or overloads.
  • Trusting API responses with as MyType: Validate external inputs at boundaries once, then pass strongly typed objects everywhere internally.

Frequently Asked Questions

Should I enable "noUncheckedIndexedAccess": true?

Yes. It adds | undefined when indexing arrays (arr[0]) or records (map[key]), catching out-of-bounds bugs before runtime—just enable it early or fix existing files incrementally.

What is the difference between // @ts-ignore and // @ts-expect-error?

// @ts-expect-error fails compilation if the line below stops having an error, preventing stale suppression comments from piling up.

How do I keep tsc fast in a large monorepo?

Use TypeScript project references ("composite": true, tsc -b), keep "skipLibCheck": true, and avoid massive deeply nested conditional/mapped types.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Try it Yourself

Experiment with the code from this lesson in our interactive playground.

Open Playground
TypeScript Topic Hub

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum