Skip to content

Lesson 8 of 9 · 18m

Compiler Settings and Inference at Scale

The tsconfig choices, utility types, and inference habits that keep a large typed React app fast, honest, and refactorable.

TypeScript in React does not fail all at once. It erodes: a little any here, a loose tsconfig there, a few // @ts-ignore comments during a release crunch. Six months later the compiler is decoration and refactoring is archaeology.

Start with the honest baseline

{
  "compilerOptions": {
    "strict": true,
    "noUncheckedIndexedAccess": true,
    "exactOptionalPropertyTypes": true,
    "jsx": "react-jsx",
    "moduleResolution": "bundler",
    "skipLibCheck": true
  }
}

strict is the floor. noUncheckedIndexedAccess catches the array[0] assumptions that blow up on empty results. exactOptionalPropertyTypes distinguishes "missing" from "present but undefined" — subtle, but important in component APIs.

Know the widening traps

const options = ["small", "large"];          // string[]
const sizes = ["sm", "md"] as const;        // readonly ["sm", "md"]
type Size = (typeof sizes)[number];          // "sm" | "md"

as const is useful for finite UI choices. Do not confuse it with a cast that silences type checking — as User is an assertion, not evidence.

Use satisfies to check shape without widening

const routes = {
  home: "/",
  user: "/users/:id",
} satisfies Record<string, string>;

satisfies validates the object while preserving the precise literal type. It is one of the best modern TypeScript features for application configuration and route tables.

Indexed access and inference are your refactoring allies

type User = Awaited<ReturnType<typeof fetchUser>>;
type UserRole = User["role"];

Deriving types from implementation keeps boundaries synchronized. But do not infer everything from everything: public APIs deserve explicit names so errors read like your domain instead of a type-system ransom note.

Control unsafe escapes

  • Prefer unknown over any; it forces validation before use.
  • @ts-expect-error is better than @ts-ignore because it fails when the underlying issue is fixed.
  • Ban new casts in lint where practical (@typescript-eslint/no-explicit-any, consistent-type-assertions tuned to your codebase).

Field notes

  • Type errors should be readable from the use site backward; if not, add named intermediate types.
  • Do not enable strictness flags globally in one heroic PR unless the codebase is tiny — migrate deliberately.
  • skipLibCheck is pragmatic, not a confession of defeat; dependency types are not your product.
  • Fast editor feedback is part of type architecture. Giant unions and deeply recursive types can make TypeScript feel broken even when correct.

Was this lesson helpful?

Sign in to rate