Learning: NodeJS and TypeScript
Hi All,
It’s been a while since I’ve done a blog post. Back in April I moved cities from Wellington to Auckland (New Zealand) and started a new role in a completely new stack. Its time to come up for some air and talk about some of my thoughts on the tech.
At the time of writing, my role involves something quite new for me - TypeScript, Node.js and more! At my company, I have been tasked with creating and/or extending various fringe services. These are typically written in TypeScript running in a Node.js environment. So what have my experiences been so far?
Node.js
Node.js (or node for short) has been interesting to use again. It’s quite hard to avoid npm (the package manager in node environments) if you do most other types of dev. Usually, some tool you use is distributed by perhaps brew, or increasingly commonly in npm. I’ve used npm a bit before when I worked on a previous employers SDK. I was new to nvm which is the node version manager, which I now use to change between versions of node. This is helpful for me when switching to more legacy applications that are tied to an old version of node.
I did have one of my mental models corrected, which I think is of note. I originally always thought node was a web framework similar to Rails, but it’s not that. It’s an environment in which javascript on the server. Not much else more (not to diminish that, the whole of node is impressive). A common framework used on top of this is Express, Koa or Fastify. These are mainly there to run web servers though and don’t include something like Rails’ ActiveRecord out of the box.
So yeah, node seems great and quite useful. I’ve not experienced any sort of “javascript is slow” yet, but most of my applications are queue-driven… so performance isn’t really a factor. I’ve yet to see much of a downside yet. Apart from having to use JavaScript itself, which leads me into:
TypeScript
TypeScript is a dream. As a person who has worked almost exclusively and loves compiled typed (and type-checked) languages like Swift or C#, TypeScript is a nice layer on top of JavaScript. It’s fairly easy to set up – a single config file with a bunch of options and sensible defaults – and then I found my self easing into adding the syntax in. I first saw TypeScipt back at CodeCamp Wellington, and it gave a good impression then, and its followed through.
Out of the box it does a lot of type checking, so instead of the loosey-goosey ‘everything is a bag of properties’ like you’d see in regular javascript, you start to think in terms of ‘what shape is this object’ - and you can declare an interface for it, and then deviating from that will give you a warning. This suddenly fixes incorrect methods and properties of objects, giving you a lot of safety and removing a whole class of runtime exception. (And this is just the start of a typing rabbit hole of awesome one could go down) I won’t go into a full blow tutorial, but I think I’m comfortable in asserting that if you’re doing JS full time or even part-time, you should swap it out with JS. Unlike previous efforts in this space (eg CoffeeScript) TypeScript is backed and the community is strong, lead by Microsoft.
I’ve really enjoyed getting to use these technologies that are new to me, and my side projects have benefited! I know what a promise is now, what async/await does, what destructuring is, and much of the JS standard library. JavaScript is really quite expressive, and I hope to improve on these skills writing better more idiomatic JavaScript as time goes on.
At this point, I must shout out a colleague, Jacob Marshall, who has given me a lot of his time to bring me up to speed on this stuff, and breaking down some of the trickier stuff. Jacob is a big proponent of JS, so if you’re into JS, he’s worth a follow.
So that’s my first post in what I hope to be a small series about the tech I’m playing with these days. I’m always learning new stuff, I just hope to find time to tell you all what it is I’m doing :)
‘Til next time!
Sam