JavaScript gives you too many ways to shoot yourself in the foot. Just what youโd expect from a programming language that was created in a week.
The JavaScript programming language has a lot of history, but it was created over about โ yes โ a week in 1995.ย It was first called LiveScript, but the name was changed to JavaScript to ride the bandwagon for Java, despite having nothing to do with Java.ย It soon became very popular, enabling the web application revolution and drawing support by practically every web browser on the planet. Today JavaScript may be the most commonly used programming language in the world.
Iโm not a fan.
Iโve said for years that JavaScript is to the web browser what assembly code is to the CPU. Sure, assembly is a powerful language, just as JavaScript is, but we have higher-order languages for a reason.ย And given that the browser is the real operating system these days, JavaScript should โ and eventually will โ go the way of assembly in the minds of developers.ย
I donโt deny JavaScriptโs success and effectiveness. The web is built on it, and people have done amazing things with it despite its substantial shortcomings. But I just canโt bring myself to think of JavaScript as a language that we should be writing, just like only a precious few developers write assembly language today.
I prefer having a compiler write my assembly language when building Windows applications, and I prefer having compiled TypeScript write my JavaScript for me when building web applications.
TypeScript is another story. Now there is a programming language.ย Designed by the great Anders Hejlsberg, TypeScript has all of the benefits of JavaScript (such as they are) while adding a type system that is expressive and powerful.ย Frankly, I donโt understand why anyone would prefer JavaScript over TypeScript.
Why donโt you use TypeScript?
First, you can start with TypeScript at your own pace. Itโs not all or nothing. All JavaScript code is TypeScript code.ย I (mostly) joke that I will be happy to charge a nice fee to convert all JavaScript development teams to TypeScript developers by simply changing all their *.js files to *.ts files.ย With that one change, they would become a TypeScript shop, and they wouldnโt have to change how they wrote code at all.ย They could then gradually start using TypeScript as they want.ย
My lack of understanding further stems from the lame objections that developers make to using TypeScript.ย
โAll those types just get in the wayโ
Okay, sure, they may get in your way. You can maybe knock out something quickly and know everything about the code you wrote.ย But what about the poor sod who has to try to figure out what you were thinking when they look at your code from six months or a year ago?ย (Never mind that the poor sod is probably going to be youโฆ)ย This is the same thing as saying, โI want to have unforeseen problems in a year when I canโt remember what all of this code is supposed to be doing.โ
Typing everything allows you to clearly and succinctly declare intentions with your code as well as enforce those intentions throughout your code base. For an application with many developers, being able to clearly and definitively express what code does is a huge advantage over code that requires a fellow developer to spend cognitive energy to figure that out.
โJavaScript is great for quick prototypesโ
Okay, thatโs fine, but consider this:ย We all know that the notion of a โprototypeโ is kind of a joke.ย Itโs sadly rare when a prototype is tossed aside and the โrealโ application is started again. Prototypes become the real application, and you are forever stuck with the bad foundational decisions you made when slapping together that prototype that you said you wouldnโt ever deploy but that you end up deploying anyway.ย The ability to quickly slap something together is not a virtue.
โJavaScript is good for beginnersโ
When I hear this one, I think, โI want budding developers to learn to code with bad habits and by not learning how a real programming language works.โย
โMy fingers get tired from all that extra typingโ
I mean, come on. This is the single worst excuse Iโve ever heard a developer give and Iโm embarrassed for developers who seriously put this forth. You can type now when you build the system or you can do a lot more typing later when you have to maintain, redo, and fix it.ย Not wanting to write clear, clarifying code because itโs too much typing is absurd and lazy.ย All that extra keyboard work gives you a full expression of what your code is doing.ย You can type a bit more.ย Good grief.
โThe TypeScript compiler only finds minor errorsโ
Uhm, yes. Exactly right.ย
โAll these types create too many errorsโ
Uhm, yes. Exactly right.
Okay, those last two comments were a bit flippant. But the errors are the point.ย The TypeScript compiler will find errors that could make it to deployment if not caught via testing.ย It is always better to find a problem earlier in the development cycle, and nothing is earlier than a little red squiggly showing up immediately as you type an error.ย
As for โToo many errors,โ well, thatโs a feature.ย TypeScript is precise, and precision is good and desirable when coding.ย Thereโs no limit to the ways you can shoot yourself in the foot with JavaScript.ย I see way too many โGuess what this JavaScript code would output!โ problems.ย If there is ambiguity and imprecision in a language, that language will result in buggy code. If you have to run code to know what its output should be, you have bad code.ย
โI can use unit testing to make sure my code works properlyโ
This is one argument that gives me pause.ย I love unit testing and test-driven development, and I think we all ought to be writing our code that way, so this argument is compelling.ย But then I remember that you can do unit testing with TypeScript as well, so this argument kind of falls apart.
Bad programming language, bad code
When you say โI can develop quickly with JavaScript,โ I hear โThis project is going to be a maintenance nightmare.โย When you say โI donโt like all that verbosity. JavaScript is simpler and more concise,โ all I hear is โI like code to be inscrutable when I come back to it.โ When you say โI just want to solve the problem and not deal with all those types,โ I hear โI just want to create a huge pile of problems for myself later.โ When you say โItโs hard to put types on everything,โ I hear โWah, wah, wah, wah.โ
Ultimately, JavaScript was the right thing at the right time. It ended up being folded, spindled, and mutilated to serve purposes that it isnโt well suited for.ย Thatโs why TypeScript came along.ย TypeScript leverages the ubiquity of JavaScript while adding all the power of a modern typing system.ย And that is why you should be using TypeScript instead.


