TypeScript is great! But is it really? Two studies take a deeper look.
Anecdotally, TypeScript has been the best thing that's ever happened to my code and my team. Makes our code easier to navigate, helps us better communicate intent, and prevents common bugs that creep into large codebases with many engineers.
I like to say that TypeScript, used diligently, does 80% of what you'd write unit tests for:
- can't call functions that don't exist
- can't use the wrong parameters
- catch typos
- find semantic regressions
TypeScript can't catch logic regressions. You need unit or integration tests for that.
A semantic regression happens when you change the arguments in a function or move things around and forget to update a file. A logic regression happens when you change the logic and something relies on the old logic. This is often a sign of architectural complexity or too much DRY with too little separation of concerns.
This distinction is important. TypeScript can lull you into a false sense of security.
Software engineering researchers have been conflicted about static typing since the 1960's. Is it worth the effort? Does it even help?
Somewhat famously, Hanenberg performed a controlled experiment about static and dynamic type systems at UC Irvine in 2010. The experiment found that students using the statically typed version of his language were slower at writing code than students using the dynamic version. And the code wasn't much better or less error prone.
But maybe that's students. What does real world data look like for the languages we care about?
That is to say: If you had static typing, would you have shipped this bug to production?
After analyzing 400 bugs across 389 projects, Gao et al found that static types would've prevented 15% of shipped bugs. They found no significant differences between Flow and TypeScript, but Flow annotations were quicker to add.
2017 was a long time ago and TypeScript has improved. Plus we now have a whole ecosystem of fully typed projects and dependencies. Surely that helps?
- Do TypeScript applications exhibit better software quality?
- Do TypeScript applications that avoid
anyexhibit better software quality?
TypeScript leads to 2x fewer code smells and 5x lower cognitive complexity per line of code. We were right!
BUT! TypeScript does not lead to fewer bugs or makes them faster to resolve. Even worse – TypeScript projects have 60% more bugfix commits. Implying they shipped more bugs and had to fix them later.
Those bugs also took longer to resolve. I think that's because TypeScript prevents easy semantic bugs and all you are left with are the harder-to-fix logic bugs.
And for us anti-any aficionados: No conclusive results. Once you're in TypeScript and use it diligently, the
any type has no significant measurable impact on quality. My gut says there's a tipping point for this and most TypeScript projects don't reach it.
Use types, mostly descriptive, don't hope for magic.
Static types nudge you towards writing cleaner code, make your code easier to read, and prevent easy bugs. You'll be left with the hard bugs.
I write articles with real insight into the career and skills of a modern software engineer. "Raw and honest from the heart!" as one reader described them. Fueled by lessons learned over 20 years of building production code for side-projects, small businesses, and hyper growth startups. Both successful and not.
Subscribe below 👇
Join Swizec's Newsletter and get insightful emails 💌 on mindsets, tactics, and technical skills for your career. Real lessons from building production software. No bullshit.
"Man, love your simple writing! Yours is the only newsletter I open and only blog that I give a fuck to read & scroll till the end. And wow always take away lessons with me. Inspiring! And very relatable. 👌"
Senior Mindset Book
Get promoted, earn a bigger salary, work for top companiesLearn more
Have a burning question that you think I can answer? Hit me up on twitter and I'll do my best.
Who am I and who do I help? I'm Swizec Teller and I turn coders into engineers with "Raw and honest from the heart!" writing. No bullshit. Real insights into the career and skills of a modern software engineer.
Want to become a true senior engineer? Take ownership, have autonomy, and be a force multiplier on your team. The Senior Engineer Mindset ebook can help 👉 swizec.com/senior-mindset. These are the shifts in mindset that unlocked my career.
Curious about Serverless and the modern backend? Check out Serverless Handbook, for frontend engineers 👉 ServerlessHandbook.dev
Want to Stop copy pasting D3 examples and create data visualizations of your own? Learn how to build scalable dataviz React components your whole team can understand with React for Data Visualization
Did someone amazing share this letter with you? Wonderful! You can sign up for my weekly letters for software engineers on their path to greatness, here: swizec.com/blog
By the way, just in case no one has told you it yet today: I love and appreciate you for who you are ❤️