Skip to content

12 TypeScript Code Smells to Fix Before Your Next Refactor

TypeScript anti-patterns like `any` abuse and interface bloat cost teams real debugging time. Here are 12 code smells with before-and-after fixes.

· · 12 min read

Updated: November 27, 2026

TypeScript example replacing an as User cast with an isUser type guard that checks the email field

Quick Take

TypeScript anti-patterns are sneaky. They don't always throw compiler errors, they just make your codebase harder to maintain, harder to refactor, and harder to hand off to a new engineer. I've catalogued 12 of the most common ones, grouped by category, with real before-and-after examples.

Every TypeScript project starts clean, and then the deadlines arrive. Someone adds any, another person duplicates an interface, and six months later the code smells are everywhere and you're afraid to refactor because nothing is clear anymore.

The patterns that cause the most pain in a large TypeScript codebase are the same 12 things, showing up over and over. You've probably written some of them. I definitely have.

When to use this vs the patterns guide: This post catalogs what to remove, the 12 anti-patterns that quietly degrade a TypeScript codebase. If you're starting from a clean slate or want to know which positive patterns to adopt, the 15 TypeScript clean code patterns guide is the companion piece. Use this one as a code-review checklist; use the other as a design playbook.

TypeScript type safety anti-patterns (1-4)

A TypeScript type safety anti-pattern is any coding habit that compiles cleanly but quietly disables the compiler's checks, leaving bugs that only surface at runtime. Four patterns do most of the type safety damage: any used as an escape hatch, type assertions that paper over uncertain data, non-null assertions that promise something the runtime never verifies, and vague catch-all types like object or {}. The compiler can only protect code paths it can actually reason about, and each of these four patterns removes a path from that reasoning. The fix is rarely more code, it is usually one narrower type. Below, each pattern gets a before-and-after example plus a short note on what the "fix" version lets tsc --strict catch that the smell hides. Twelve total anti-patterns appear across this guide, and these four are consistently the ones new team members introduce first, because they are also the fastest way to silence a red squiggly line without understanding why it appeared.

1. Using any as a Type Escape Hatch

any turns off type checking entirely. It spreads silently, one any parameter means the return type is often inferred as any, and suddenly half your codebase has no type coverage. Replace external or untyped data with unknown, then narrow it explicitly before use.

// Smell: any disables all checks
function parseConfig(raw: any) {
  return raw.database.host; // no error even if missing
}

// Fix: unknown forces you to check first
function parseConfig(raw: unknown): string {
  if (
    typeof raw === 'object' && raw !== null &&
    'database' in raw && typeof (raw as any).database?.host === 'string'
  ) {
    return (raw as { database: { host: string } }).database.host;
  }
  throw new Error('Invalid config shape');
}

To see exactly how much any hides, I appended one obviously-broken call to each version above (cfg.toUpperCase().thisMethodDoesNotExist()) and ran tsc --strict --noEmit on both:

$ npx tsc --strict --noEmit smell.ts    # the any version
$ echo $?
0

$ npx tsc --strict --noEmit fix.ts      # the unknown version
fix.ts(12,31): error TS2339: Property 'thisMethodDoesNotExist' does not exist on type 'string'.
$ echo $?
1

The any version compiles clean, exit code 0, on a line that would crash the moment it ran. The unknown version's compiler catches it before the code ever executes. The bug is identical in both files, and one type annotation decides whether you hear about it.

2. Type Assertions Hiding Real Bugs

as SomeType overrides whatever the compiler worked out, and nothing checks the claim at runtime. Put as User on an API response that sometimes returns an error object and it works in dev, then crashes in prod when the API changes.

as SomeType is the duct tape of TypeScript. It tells the compiler to stop thinking and trust you, and the problem is you're often wrong.

Share this Post on X Bluesky
// Smell: casting away uncertainty
const user = apiResponse as User;
console.log(user.email.toLowerCase());

// Fix: validate the shape at runtime
function isUser(val: unknown): val is User {
  return (
    typeof val === 'object' && val !== null &&
    typeof (val as User).email === 'string'
  );
}
if (isUser(apiResponse)) {
  console.log(apiResponse.email.toLowerCase());
}

3. Non-Null Assertions Instead of Real Guards

The ! operator removes null and undefined from a type without adding any runtime check. It's a promise to the compiler. If that promise breaks, say, a DOM element doesn't exist yet, you get a runtime crash with no type trail to follow.

// Smell: asserting existence without checking
const button = document.querySelector('#submit')!;
button.addEventListener('click', handleClick);

// Fix: guard before use
const button = document.querySelector('#submit');
if (button) {
  button.addEventListener('click', handleClick);
}

4. Overusing object or {}

object accepts anything non-primitive. {} accepts literally everything except null and undefined. Neither gives you useful type information. When you find yourself writing Record<string, object>, stop, what shape does that object actually have?

The compiler won't flag either one: object and {} are legal annotations, so strict: true and noImplicitAny (both covered in the TypeScript strict mode guide) pass them without comment. Lint is what catches {}: the @typescript-eslint/no-empty-object-type rule reports it and is part of the recommended config.

// Smell: structureless objects
function renderWidget(config: object): void { ... }

// Fix: describe the actual shape
interface WidgetConfig {
  id: string;
  label: string;
  onClick?: () => void;
}
function renderWidget(config: WidgetConfig): void { ... }

Interface and type anti-patterns (5-7)

Interface bloat refers to combining multiple unrelated concepts into a single interface until it grows past the point where any one consumer needs most of its fields. How you organize types matters as much as what you put in them, because TypeScript is structurally typed, two interfaces with the same shape are interchangeable even if they represent completely different concepts. That structural looseness is a feature when you want flexibility, but it becomes a liability the moment two teams independently define UserSummary in different folders and let them drift. The typical case is one wide interface passed through every layer of a flow, with each layer touching only a few of its fields, because splitting it felt like more work than living with it. Three anti-patterns dominate this category: interface bloat, duplicate types across files, and enum misuse where a union type would be lighter and safer. Structural typing is a deliberate design choice (the Handbook page linked above says it was designed around how JavaScript code is typically written), which means the compiler will never stop you from creating near-duplicate shapes on its own, the discipline has to come from the team.

5. One Giant Interface (Interface Bloat)

A 40-field interface is a red flag. It usually means you've combined multiple distinct concepts into one object because they happen to travel together. This makes partial updates painful and forces every consumer to handle fields they don't use.

// Smell: one interface for everything
interface UserData {
  id: string;
  email: string;
  firstName: string;
  lastName: string;
  role: string;
  permissions: string[];
  lastLogin: Date;
  avatarUrl: string;
  billingAddress: string;
  subscriptionTier: string;
  // ... 12 more fields
}

// Fix: split by responsibility
interface UserIdentity { id: string; email: string; }
interface UserProfile { firstName: string; lastName: string; avatarUrl: string; }
interface UserAccess { role: string; permissions: string[]; }

6. Duplicate Types Across Files

Type duplication is harder to spot than code duplication because TypeScript is structurally typed, two identical interfaces don't cause errors. But they diverge. Someone updates UserSummary in dashboard/types.ts but forgets the one in reports/types.ts. Now you have a subtle mismatch and no error.

Have you ever fixed a bug in one place only to find it still crashes somewhere else? That's often duplicate types at work.

Move shared types to the module that owns the data and import them from there instead of copying them.

7. Enum Misuse When Union Types Are Better

TypeScript enums generate real JavaScript objects, and the handbook itself says an as const object often does the job. They're great for numeric values and reverse mapping, but string enums are almost always better expressed as union types, and that's still true now that TypeScript 7 is out (see the migration guide linked below). Union types produce zero runtime output, work natively with JSON, and are easier to refactor.

// Smell: string enum adds runtime JS you don't need
enum Status {
  Active = 'active',
  Inactive = 'inactive',
}

// Fix: union type is simpler and lighter
type Status = 'active' | 'inactive';

Smells in functions and async logic (8-10)

Function and logic anti-patterns make async code brittle and turn return contracts into a guessing game for anyone calling the function later. Three show up constantly: nested .then() chains that predate async/await becoming standard, ?. chaining used so liberally it hides genuine data integrity problems, and return types like string | null | undefined that force every caller to write defensive code without knowing which branch actually matters. All three can reach production with the compiler technically satisfied, because it is not being asked the right question. Per the TC39 finished proposals list, optional chaining and nullish coalescing both reached stage 4 in December 2019, so there is no excuse rooted in tooling age, teams that overuse ?. today are choosing convenience over clarity. A five-minute code review catches most of these: does the return type actually tell the caller what to expect, and does the async chain read top to bottom without three levels of nested callbacks in the middle?

8. Callback Hell in Async TypeScript

Node.js has run async/await without a flag since 7.6.0 (February 2017). Nested .then() chains still turn up in TypeScript codebases, sometimes three or four levels deep. They're harder to read, harder to type correctly, and error handling becomes ambiguous, and a flat async function fixes all three.

// Smell: nested promise chains
fetchUser(id)
  .then(user => fetchOrders(user.id)
    .then(orders => processOrders(orders)
      .then(result => saveResult(result))));

// Fix: flat async/await
async function loadUserData(id: string) {
  const user = await fetchUser(id);
  const orders = await fetchOrders(user.id);
  const result = await processOrders(orders);
  return saveResult(result);
}

9. Optional Chaining Overuse Masking Real Bugs

?. is great for genuinely optional data. It's a problem when you use it on data that should always be present. user?.profile?.name looks defensive but it means "I'm not sure if profile always exists." If profile should always exist, you've hidden a data integrity problem under a silent undefined.

// Smell: hiding assumptions with optional chaining
const name = user?.profile?.name ?? 'Unknown';

// Fix: be explicit about what's required
// If profile is required, define it that way:
interface User {
  profile: UserProfile; // not optional
}
const name = user.profile.name; // error if profile is missing from data

10. Unclear Return Contracts

A function that returns string | null | undefined with no documentation is a maintenance problem. Callers have to dig into the implementation to know what to check for. Pick one falsy sentinel, or better, throw on failure and return a non-nullable value on success.

// Smell: ambiguous return type
function findUser(id: string): User | null | undefined { ... }

// Fix: clear contract
function findUser(id: string): User | null { ... }
// Or for non-nullable: throw if not found
function getUserOrThrow(id: string): User { ... }

Architecture anti-patterns: who owns a type (11-12)

If you want a fuller checklist for catching these in review, our TypeScript quality framework packages these smells into a graded scorecard you can run against any new PR.

Architecture anti-patterns rarely cause an immediate compiler error. Instead they cause slow, painful refactors six months from now, when someone finally has to untangle a module graph that grew organically without anyone deciding where a given type actually belongs. A single types.ts dumping ground and circular type dependencies between modules are the two most common offenders, and both share the same root cause: nobody assigned clear ownership to a shared concept early enough. Module ownership is the practice of making sure every type is defined in the same module that creates or manages the underlying data, rather than in a generic shared file that everyone imports from and nobody maintains. In a large codebase these two patterns are a steady source of onboarding friction: a new engineer has to work out which of several Order types is the real one before they can change anything. Fixing this after the fact takes longer than avoiding it, but it is still worth doing, because the alternative is a codebase where every refactor requires archaeology first.

11. The types.ts Dumping Ground

A single src/types.ts file with 300 interfaces is the file everyone imports and no one owns. What starts as "one place for all types" becomes a hub that makes every module depend on every other module indirectly. Circular import warnings start appearing. Refactoring one type breaks six unrelated things.

The fix is boring but it works: types belong in the module that creates the data. UserRepository defines User. OrderService defines Order. Other modules import from those sources directly.

12. Circular Type Dependencies

Type A imports Type B which imports Type A. TypeScript often allows this at the type level, so you won't see an error, but bundlers and the module graph hate it. It signals that your module boundaries aren't real. Two modules that mutually depend on each other's types are really one module pretending to be two.

When you find a circular type dependency, ask: which module truly owns this shared concept? Move it there. Both other modules import from that single source.


All twelve are the predictable result of building fast without pausing to ask "who owns this type?" or "what does this function actually promise to return?" The compiler alone won't find most of them, whether you're still on 5.x or already migrated to TypeScript 7, because every one of them type-checks. strict: true and noUncheckedIndexedAccess cover implicit any and unchecked access; explicit any, ! and {} are jobs for lint rules such as @typescript-eslint/no-explicit-any and @typescript-eslint/no-non-null-assertion, and the ownership problems are jobs for code review.

Start with the type safety group, fix any and type assertions first. The architecture patterns can wait until your next refactor sprint. But fix them you should, before the codebase becomes the thing everyone is afraid to touch.

All 12 anti-patterns by fix priority

CategoryAnti-patternsFix priority
Type safety (1-4)any escape hatch, hiding bugs with assertions, non-null assertions, overusing object/{}Fix first
Interfaces and types (5-7)Interface bloat, duplicate types, enum misuseFix during refactors
Function and logic (8-10)Callback hell, optional chaining overuse, unclear return contractsFix during refactors
Architecture (11-12)types.ts dumping ground, circular type dependenciesCan wait for next refactor sprint

Frequently Asked Questions

What is the most common TypeScript anti-pattern?
Using any as a type escape hatch is the single most common TypeScript anti-pattern. It disables type checking entirely on that value, which defeats the purpose of using TypeScript. Replace it with unknown for untyped external data, then narrow the type before using it.
Why are non-null assertions (!) considered a code smell?
Non-null assertions tell the compiler 'trust me, this value exists', but they don't add any runtime check. If you're wrong, you get a null reference error in production, not a compile-time error. That's what makes it worse than a missing type: the code looks checked when it isn't. Use optional chaining or an explicit guard instead.
When should I use union types instead of enums in TypeScript?
For string-based constants that never need reverse mapping or numeric values, union types are almost always the better choice. type Status = 'active' | 'inactive' | 'pending' is simpler than an enum, produces no runtime JavaScript output, and works natively with JSON data. Enums generate real JavaScript objects, which can cause unexpected behavior with string comparisons and tree-shaking.
What is the types.ts dumping ground anti-pattern?
A single types.ts file that holds every interface, type alias, and enum in a project is a maintenance problem. It creates a hub that nearly every file imports from, making refactoring painful and hiding which types are actually related. Move types to the modules that own them, a User interface belongs in the user module, not a global file.