Why (almost) every JavaScript developer should learn TypeScript
15 minAudio
Max explains why he thinks that most JS developers benefit from learning TypeScript.
Audio and video play straight from the show's own hosting. PodSubs stores and serves no media.
Transcript
From the show. Tap a time to play from there.
I believe that for most JavaScript developers, it makes sense to learn TypeScript and they probably should learn TypeScript, though, of course, I'll say it right away in case it isn't obvious, this is just my opinion. If you don't like TypeScript, you don't need to use or learn it, but I think there are good reasons for using or learning TypeScript and this is what I'll try to explain over the next minutes. And if I take a look at the numbers, the
number of people working with TypeScript in the last Stack Overflow survey, for example, and the growth of that number, for example, then I would guess many people would agree. It's probably fair to assess that TypeScript is pretty popular and is growing in popularity because otherwise people would probably not be choosing it for their projects or at least it seems to
offer substantial advantages. Now at the same time, I'm well aware that last year in 2023, we had a period in that year where some big projects were dropping TypeScript and we had a blog post by DHH, the creator of Ruby on Rails, who shared a pretty strong opinion against using TypeScript and why using TypeScript is bad.
Now just to make sure that we're all on the same page, the idea behind TypeScript, the main idea, the main thing it brings to the table is, of course, that it adds static typing to JavaScript, which in the end means that when you are writing JavaScript code, you can't suddenly change the type of a value that's stored in a variable and you can also, for example, predefine the type that should eventually be stored in a variable.
You can be clear about the type of value that's expected to be returned by some method or function and in general, the main idea simply is that you can be specific about which type of value goes where in your code and that you can't accidentally use the wrong type of value, for example. And it's not just about the wrong type, like a number being used in a place
where a string is expected, though that can also be useful. It's also about the many places in JavaScript where a value might be undefined or null and you forgot about this scenario when writing your code and therefore suddenly your application, your website crashes when it's running in a certain scenario because such an undefined or null value was received. It's cases like this where TypeScript can
help you because it makes you aware of such situations during development. It tells you if you are using a value in a certain place where it could potentially be undefined so that you can improve your code to handle that scenario where a certain value might be undefined and that can lead to better code, to code that contains fewer bugs and that hopefully doesn't crash at runtime that often.
So TypeScript really brings some advantages to the table, at least in my opinion and as it seems, as I mentioned initially, also in the opinion of many other developers. Now at the same time we had for example dhh dropping TypeScript in his Turbo library because he complained about unnecessary type gymnastics being required when trying to write correct TypeScript code because that's
also important. You can of course, when working with TypeScript, often take shortcuts and use the any type or the exclamation mark to tell TypeScript that you know that a certain value can't be undefined in that place where TypeScript thinks it could be undefined. You can use such shortcuts to avoid writing complex types or extra checks to
rule out certain types. And it's especially that part where you sometimes might need to write complex types which typically means complex generic types which you really have to learn to get right. It's situations like this where you might feel inclined to drop TypeScript because yes, this can be complex and annoying and introduces a new error source
because if you get a type wrong, you might start using it incorrectly and you might start using some value incorrectly and in the end it does have a pretty steep learning curve to be able to write good generic types. So yes, that can be annoying. However, this is mostly a problem library authors will face because if you are writing a library that's meant to be used by other developers in their projects, you
might want to use TypeScript to help those developers in case they are using TypeScript in their projects so that your library gives them proper types and works properly in their library. So you might want to use TypeScript when writing your library and it's indeed in such situations where you often need to write complex types. And the reason for that is that if you're working on a project, not on a library, you often don't need super complex
types because you can often rely on type inference and also because you don't need to write very generic abstract functions or value types that can be used in a broad variety of situations. Instead you often have one specific situation, one specific place in your code where a certain value is only used in one very specific way and therefore you don't need to set up a super flexible,
complicated type to support all other kinds of value types. If you're building a library on the other hand, you typically need to do that because you want to offer flexibility in many places so that your library can be used in a broad variety of different projects. And therefore indeed if you are writing code for a library, you can have a hard time from time to time when working with TypeScript.
Now I would argue it's probably still worth it because for one, your library being written in TypeScript of course also gets the benefits of using TypeScript, which for example means that certain errors can probably be caught early and fixed. It also means that your library might be more interesting for other developers who are using TypeScript in their projects. It might also help with collaboration and finding collaborators because whilst
TypeScript could also be an extra hurdle, it might also mean that you're getting higher quality collaborators who help you working on the library. But of course ultimately, it is something that's totally up to the library author and I can fully understand if you don't feel like doing all those type gymnastics. Though the super important thing here really is that I'm now talking about libraries.
As I mentioned before, if you are working on some project, on some web application, and you're just using some libraries but you're not writing one yourself, then chances are pretty high that you don't need to write a lot of complex types. You might sometimes need a bit of a more complex type here and there, but it's typically not too bad. Of course, always depending on the exact project you're working on, on the
size of the project and so on. But chances are high that you won't have a too hard time when using TypeScript in such a project. And that's why I believe that every developer, library authors excluded to some extent, should really consider using TypeScript and learning TypeScript therefore because there are real benefits to be reaped and the price, if you're not one of those library authors,
really isn't too high, in my opinion. And indeed, the opposite is the case, I would say. If you know how to work with TypeScript, you become more employable, both for a fixed employment position but also as a freelancer. Because if they're not using TypeScript, that's okay. You do know how to work with vanilla JavaScript after all, since TypeScript just builds up on that.
But if they do use TypeScript, you have an advantage compared to developers who don't. So from that perspective, it also makes a lot of sense to learn TypeScript. The question then just is how good you have to become at TypeScript. You can definitely try to become a TypeScript master who's able to read and understand and most importantly write all the super complex generic
types you might sometimes need in certain libraries. But chances are that that's not something you have to do because for all the reasons I mentioned before, where if you're not working on a library, you might not need those super complex generic types. Now of course, using TypeScript has one other disadvantage besides those complex types you sometimes need to write. And that disadvantage is that it's not
running in the browser without an extra compilation step. So if you're writing TypeScript code, you can't just take that code and upload it to some server and call it a day. It will not work once it's been loaded into the browsers of your users. And as a side note, that's a bit different if you're using TypeScript for server side code, for example, because indeed there we have solutions like bun, which is a
JavaScript and TypeScript runtime or extra packages that indeed do allow you to execute TypeScript code just like that without a compilation step. But if you're writing client-side JavaScript code, it won't work just like that. And that of course can be a disadvantage if you have maybe a pretty simple project, which otherwise wouldn't require a compilation step and wouldn't require a build tool that
compiles your code and optimizes your code. Or then you still need one because you're using TypeScript. So that need for that extra compilation step and build step, that of course can be annoying in certain situations. Now again, my feeling here is, and what I see out there, that for many projects, probably most projects, front-end JavaScript projects, you have a build tool
and a build step anyways. You're using something like Vite or Webpack anyways. And then adding TypeScript shouldn't be a huge deal, to be honest, because you have that process anyways. Why not just add in that compilation step? Typically it shouldn't be too complicated. So that might be a reason for some people. For example, it was a reason for the Svelte team, which also dropped
TypeScript and moved to JS docs, which is another way of adding types that does not require a build step, but also isn't as ergonomic in my opinion. But it's probably not an argument for many other developers who have a build step, who have a build tool in their project anyways, because there it really doesn't matter too much. Typically compilation also doesn't take super long. And of course, as I mentioned, we also have situations where you might not
need one at all despite using TypeScript. If you are, for example, executing your code with a tool or in an environment that natively supports TypeScript, though there you always might want to at least question whether you possibly could have a performance disadvantage by running uncompiled TypeScript. That could be a valid concern there and is something you should definitely check and make sure that it isn't. But for all those reasons, I do believe
that most developers should learn and use TypeScript because learning it honestly isn't too complicated if you don't get into those super complex types, at least initially. And therefore, then you just get a lot of benefits, in my opinion, without too much extra work. And indeed, once you've worked with TypeScript a bit, you will see that actually you don't have to add too many
types in your code base because often you can rely on type inference anyways, and then you just get the benefits almost without any additional extra work. And that's why I personally love TypeScript and why I'm using it in essentially all my projects and why I recommend the same for most other developers.
GPT 6 Astra was released. It's a good model, I have to say. Especially for 3D work it seems to be a real game changer. But due to all the hype, you may be disappointed...