DEV Community

Cover image for Phaser: Moving to TypeScript
Núria
Núria

Posted on • Edited on

Phaser: Moving to TypeScript

Originally published at Medium on Apr 17, 2019

I’m not a fan of TypeScript. Anyone that has worked with me, follows me on Twitter or been near me has probably heard my rants. It’s not that I’m against statically typed languages, but I feel that TypeScript tries to fix something that is not broken. Yes, I strongly believe that JavaScript is not broken. Most people that keep complaining about JavaScript has not taken the time to understand the language.

But yeah, TypeScript. It’s supposed to make your code more robust, avoid bugs, blah blah blah. I’m a bit skeptical about all those blanket statements. And I’m not alone on that, as Eric Elliott mentions on The TypeScript Tax:

“Type safety doesn’t seem to make a big difference. TypeScript proponents frequently talk about the benefits of type safety, but there is little evidence that type safety makes a big difference in production bug density.”

By now you might be thinking “but wait, wasn’t this post titled Moving to Typescript?”. Yeah, despite all this ranting I moved the game code to TypeScript, and I don’t regret it.

The reason I switched to TypeScript, the main benefit I see over JavaScript and the one I see least mentioned when people say nice things about TypeScript, is developer experience.

You see, Phaser 3 doesn’t have the best documentation. It’s quite cumbersome to find something there, and most of the time I find it really hard to see how everything is connected. Plus, Phaser itself does not make things too easy for the developer(like functions with a lot of parameters, instead of using an object with keys which makes the code much clearer).

TypeScript really helps making this way easier and reducing trips to the docs dramatically. I can see what kind of parameter a function expects, in which order, and what is returned. Which are the available properties of a Sprite, or a Scene. It’s true that you can have type checking with JavaScript without actually having to use TypeScript in your files, but if you are doing this I think it’s worth to go all the way and use TypeScript at its fullest.

It’s not all nice and good, the truth is that Phaser and TypeScript don’t work that well together, there is even less documentation, and at the time there are no Phaser types in DefinitelyTyped (so you need to manually add them to your project, and remember to update them if you upgrade the Phaser version). But overall, it’s totally worth it.

Does this mean I want to use TypeScript in all my projects now? Hell no. If you are using a simpler framework with good documentation, I don’t think it adds as much. But for Phaser, TypeScript has saved my sanity.


I haven’t been posting on the development diary lately, but this doesn’t mean I haven’t been working on the game! If you are curious about the progress, that’s how it looks so far:

https://www.youtube.com/watch?v=i5tFT-5p_0U

Graphics and animations are not final, but they give a guideline about how it’s going to look. I’m pretty happy!

Top comments (0)