What common runtime-vs-type mismatch bugs does migrating to TypeScript often surface in previously 'working' JavaScript code?
Short answer
Migration frequently reveals latent bugs that JavaScript's dynamic typing silently allowed: functions that don't always return a value on every code path, values assumed to never be null/undefined that actually can be, and array/object shapes assumed consistent that actually…
Advertisement
AModel answer
Migration frequently reveals latent bugs that JavaScript's dynamic typing silently allowed: functions that don't always return a value on every code path, values assumed to never be null/undefined that actually can be, and array/object shapes assumed consistent that actually vary — all previously invisible until the type checker starts enforcing consistency.
function getDiscount(user) { // plain JS, silently 'worked'
if (user.isPremium) return 0.2;
// no else branch -- implicitly returns undefined, used as a number elsewhere!
}
// TypeScript: function getDiscount(user): number is flagged, since not all paths return a numberReal-world example
Uncovering a genuine, previously-silent bug where a function occasionally returned undefined and it went unnoticed for months.
Likely follow-ups
- Is it common for a TypeScript migration to actually catch real production bugs like this, beyond just adding type annotations?
Related topics