Code Buckets

Buckets of code

TypeScript

Is there any reason not to use TypeScript?

Devil: stick to JavaScript, it’s easier. Angel: try TypeScript, you’ll get fewer bugs.

I’ve written a lot of code for side projects and for this blog, and a lot of it has been JavaScript. I’m really wondering now why I didn’t write it all in TypeScript instead. It’s easy to learn, improves your code immediately and is a superset of JavaScript hence there is a super easy migration path.

So why didn’t I use it more? Are there any reason not to use TypeScript?

Bad Reasons

Not wanting to add in another language and complicate things

On the face of it, a reasonable stance. Surely another language will just complicate matters. However, by putting in TypeScript, the language becomes more consistent and simpler in most ways. In any case, there is the escape hatch of using the ‘any’ datatype if you really can’t work out a datatype or even keeping the entire file in JavaScript.

So, adding in another language actually makes things easier. Counterintuitive perhaps but true.

Can’t be bothered setting it up

It does seem like a faff to set up TypeScript particularly if you are starting off and have just set up Node, SASS, Jest and React and now you’ve got something else to set up. You might just run out of patience. But if you are using create-react-app then it’s a simple matter of an additional flag

npx create-react-app MyApp –template typescript

Node isn’t too much harder. To be fair if you are using Angular then it’s a bit more fiddly, but you only need to do it once so it shouldn’t be a massive deterrent.

The freedom of JavaScript

JavaScript is certainly a freewheeling kind of language. JavaScript has the freedom to not instantiate variables, freedom to pass in whatever you like to functions, and freedom to have statements that are falsy rather than just being false. You’ve got the freedom to write a whole heap of bug inducing code. If you close your eyes, you can feel the winds of freedom whistling around your collapsing code base. Great!

TypeScript also has its freedoms. Freedom from type mismatch errors, freedom from code breaking comparisons and freedom from method name typo bugs. Perhaps it even gives freedom from those application crippling issues that are only discovered late on a Friday afternoon. That’s the kind of freedom I prefer.

Good Reasons

Want to learn JavaScript

When I started programming, I taught myself C. I almost never have used C professionally and I never ever have got a job based on my C expertise. But I am grateful that I started out that way. It hammered home basic principles such as memory allocation, references, and arrays. I still sometime think about pointers when coding in completely unrelated languages.

In the same vein I’m always pleased that I learnt straight JavaScript and struggled with the ‘this’ keyword, prototypes and closures. It’s basic knowledge that serves well when moving up to a higher level of abstraction. Highly recommended.

Writing demo code and want to keep it simple

When I watch React and Node Pluralsight videos it’s almost always demonstrated in JavaScript. It’s reasonable really. If I know TypeScript, then I’ll almost certainly know JavaScript (or should do) so I can mentally rewrite the demo code into TypeScript as I watch. If I know JavaScript there is no guarantee I know TypeScript, so a training video written in TypeScript might be baffling. It’s hard enough to keep up with technologies so the lower the barriers to entry the better.

You are told not to use it

Sadly, we are not masters of our own code. If you are a professional programmer then you need to comply with your employers’ technologies, languages, and standards.

It is very important to pick the best tools and technologies and I believe that TypeScript is the best choice for reducing bugs in code. However, it is so much more important to be as pleasant and cooperative an employee as you possibly can be. If your company has an unreasonable fear of TypeScript and won’t use it then don’t complain about it – instead embrace the freedom and simplicity of JavaScript. It’s not that bad.

LEAVE A RESPONSE

Your email address will not be published. Required fields are marked *