The Bug TypeScript Found

An API we integrated returned IDs as strings in the JSON response. Somewhere in the migration from JavaScript to TypeScript, a developer had typed the ID as number. The consuming code used it in arithmetic — worked fine in testing because the IDs happened to be numeric strings. Would have broken the moment an ID had a letter in it.

TypeScript caught it when we updated the type definition to match the actual API contract. Suddenly thirty-seven call sites lit up with errors. Every one of them was a real bug — a calculation or comparison that would have silently produced wrong output at runtime.

Why I Stopped Treating TypeScript as Optional

Before this incident, I treated TypeScript like a linter — nice to have, mostly in the way when I just wanted to ship something. The type errors were obstacles between me and running code. After it, I started treating type errors as the compiler telling me something true about my code.

The shift is subtle but significant. A type error isn't 'TypeScript being annoying.' It's a precise description of a contract violation in your code. The question isn't 'how do I make this error go away' — it's 'why is this contract being violated, and is the type wrong or is the code wrong?'

Key takeaways

  • Model your API response types precisely — use string for string IDs, not number, and let TypeScript propagate the correct type through your entire codebase
  • When a type error appears at a call site, trace back to the source type definition before adding a cast — the error is usually pointing at a real contract mismatch
  • Enable 'strict' mode in tsconfig — it turns on the checks (strictNullChecks, noImplicitAny) that catch the most impactful bugs

Conclusion

TypeScript as a bug-prevention system is underrated relative to TypeScript as a developer-experience tool. The autocomplete and refactoring support are nice. Catching a three-month-old bug before it reaches production is better.

Enjoyed this article?

Vivek Kumar Singh

Vivek Kumar Singh

Technical Expert · Full Stack Cloud Engineer · Tokyo, Japan