Skip to Content
Course content

222: Handling Breaking Type Changes

Click on the "Edit" button in the top corner of the screen to edit your slide content.

I've seen this happen in almost every mid-to-large scale project I've touched: a developer decides to clean up a core type—maybe renaming a property or changing a string to an object for better structure—and they think, "I'll just change the interface, and then follow the red squiggly lines in my IDE to fix everything."

The "Fix All the Red" Fallacy

The misconception is that the TypeScript compiler is your "to-do list" for refactoring. While that works for a small file, it's a nightmare in a real production codebase. Let's look at why. Imagine you have a User type used in fifty different components and services:

// Old Version
interface User {
  id: string;
  fullName: string;
}

You realize you actually need firstName and lastName separately. If you just change the type to this:

// New Version
interface User {
  id: string;
  firstName: string;
  lastName: string;
}

Suddenly, your project has 142 errors. You can't commit your code because it doesn't compile. You can't run your tests to make sure you haven't broken the logic. If you're working on a team, you've just blocked everyone else's PRs because the main branch is now a sea of red. I've personally spent an entire afternoon chasing a "simple" type change only to realize I missed one obscure edge case in a utility function, and by then, I'd lost track of what the original logic was supposed to be.

The Expand and Contract Pattern

The professional way to handle this is a strategy called "Expand and Contract." Instead of replacing the type, you evolve it in stages. This keeps the app compilable at every single commit.

Stage 1: Expand. Add the new fields, but keep the old ones. Mark the old ones as deprecated so your team knows to stop using them, but the code still runs.

interface User {
  id: string;
  /** @deprecated Use firstName and lastName instead */
  fullName: string; 
  firstName: string;
  lastName: string;
}

Now, you don't have 142 errors. You have zero. You can now migrate your components one by one. You change User.fullName to ${User.firstName} ${User.lastName} in one component, commit it, and move to the next. If you need to stop for a meeting or a bug fix, your code is still in a deployable state.

Stage 2: Migrate. This is the tedious part, but it's the safe part. You use the compiler to find every instance of the deprecated fullName and replace it with the new logic. Since you aren't fighting a broken build, you can actually focus on the logic of the migration rather than just trying to make the red lines go away.

Stage 3: Contract. Once you've confirmed that fullName is no longer being used anywhere in the codebase, you delete it. Only now do you actually "break" the type. The "contraction" phase is the most satisfying part—it's the final cleanup that leaves the codebase leaner than it was before.

It feels slower at first, but it's actually faster because it eliminates the "refactoring rabbit hole" where you spend four hours fixing types and zero hours actually delivering the feature. It's about reducing the risk of the change.




📋 Practical Task

Refactoring the Legacy Order Status Type

You are working on an e-commerce platform. Currently, the Order interface uses a string for the status, which is causing bugs because developers are typing "shipped" in some places and "Shipped" (capitalized) in others.

Your Goal: Transition the status field from a string to a strict union type 'pending' | 'shipped' | 'delivered' using the Expand and Contract pattern.

Current Code:

interface Order {
  id: string;
  status: string; // This is the problem
}

function updateOrder(order: Order, newStatus: string) {
  return { ...order, status: newStatus };
}

const myOrder: Order = { id: "123", status: "pending" };

Requirements:

  • Step 1: Create a new property called statusValue using the union type 'pending' | 'shipped' | 'delivered'.
  • Step 2: Keep the original status property but mark it as deprecated using the JSDoc @deprecated tag.
  • Step 3: Update the updateOrder function to accept the new union type for the status update, while ensuring the Order object still maintains both properties during the transition.
  • Step 4: Demonstrate how you would eventually "contract" the type by removing the old status field once the migration is complete.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.