TypeScript made this project fun!!
Tags: typescript, node, webdev, api
Published: 2022-05-28 04:13:43

So I’ve worked with Node.js in the past and I did enjoy working in it. It was familiar because it was just JavaScript as opposed to being lost in learning both the Back-End and potentially learning another language to do the work (or refresh myself on a language). My main gripe though was that I was forced to write ES5 JavaScript. For those who don’t know, Node.js for the longest time only supported up to ES5. The newest version of Node supports ES6 but at the time of this writing is not LTS so I forgoed using it. The main reason why I like JavaScript now is a lot of the features you get for using ES6. Import/Export, Template Literals, arrow functions and the like made JavaScript easier to work with and write. Also as a Front-End Engineer, I was generally using ES6 to write React code and so I found myself wishing that I could just write ES6 JavaScript.

Here comes TypeScript to save the day!

For those who aren’t aware, TypeScript is a superset of JavaScript which adds a type system to it. What TypeScript also has is a compiler which will compile (or transpile your code, I always forget) your TypeScript code into JavaScript (because things like browsers and Node only reads JavaScript and not TypeScript). In that process, you can actually instruct the compiler to compile your JavaScript code into whichever version of JavaScript you’d like so that the code you ultimately run is supported by the environment you’re using it in. This means you can use the newest features of JavaScript and the TypeScript compiler will just make it into the version of JavaScript that you need. This made working on the project so much more fun because I had access to all the ES6 features I was used to having. To be honest, I don’t think I utilized them quite often in the project but you’d be surprised how often you’d write something expecting it to work and it just doesn’t. Also, if you’re writing ES5 JavaScript, you have to learn to use modules to import and export which can be a pain if you’re not used to using them (spoiler: I’m not).

To be honest, especially as a newer TypeScript dev, it can get in the way sometimes. There’s been a few times when I’m writing code and TypeScript would slap my hand and say ‘No!’ when I’m trying to do something I felt was simple. A few times while writing my validations, TypeScript would let me know that the variable I was trying to use didn’t match the type of where I was trying to use it. This also came up a few times while trying to return the image after the image manipulation as well. I do appreciate the rigor though and it really helps keep the project from potentially being buggy. I’m sure the project would be much worse if I didn’t have TypeScript slapping my hand every so often.

Here's a link to my compiler settings I used throughout the project:


![Compiler settings for TypeScript compiler](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3qmverlph1m9mub756ux.png)<figcaption>Compiler settings for TypeScript Compiler</figcaption>

Here’s a link to the commit when I initially set up the project: https://github.com/chadstewart/you-go-backend-project/commit/5f3f1c343061ae0d652233e1b7002d2b40258e88

In the next article of this series, I face the major challenge of the project. Working with Base64 images.