Using types defeats the purpose of JavaScript

Computer code on a computer monitor

JavaScript is the world's most misunderstood programming language.

Douglas Crockford, computer programmer
Poem: A type
A type for <script>s, a type for <style>s,
A type for <div>s and <span>s,
But JavaScript's the type of code
That's not at all a fan.

I read an article on dev.to (again) that talked about how important it is to know types used in JavaScript to do Typescript.

This is already a problem. With the effort made by the browser and the creator of JavaScript to hide types from the developer, why are developers so obsessed with bringing them around?

JavaScript is weakly typed for a reason: it makes easier to read, write, understand and remember.

Let's take a look at the same example,

In JavaScript:

const input = document.getElementById("myInput");

In Typescript:

const input = document.getElementById("myInput") as HTMLInputElement;

This leads me to ask, why do I care if an object is a HTMLInputElement? How does that make the code easier to understand or write, or even read? The code itself implies that it is an input element; choosing the variable name input makes it even easier!

Now, knowing the type could be useful if you have a lot of code and you are debugging. But to read, write and understand code? I don't think you need them at all. And to add to that, there are other ways to debug without the types, like using DevTools and the built-in browser debugger.

The JavaScript code snippet above is also easier to remember compared to its Typescript counterpart.

I'm not against static typing in general; I do believe it fits lower level languages like C, C++, Java, C# etc. I just don't believe it should be used for interpreted languages like JavaScript (or Python). JavaScript is a scripting language and scripting languages should be easy to script!

Bottom line

Nobody cares about types in frontend development (and you shouldn't either)! Learn to choose clear variable names and that can take you a long way.

If you would like to reply to or comment on this blog post, feel free to email me at efe@mmhq.me.