Phaser: Moving to TypeScript
Tags: gamedev, devlog, typescript, phaser
Published: 2020-02-11 16:46:47

_Originally published at [Medium](https://medium.com/@pincfloit/game-development-diary-v-61521b8df4ef) on Apr 17, 2019_

I’m not a fan of TypeScript. Anyone that has worked with me, follows me on [Twitter](http://twitter.com/pincfloit) 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](https://medium.com/javascript-scene/the-typescript-tax-132ff4cb175b):

> “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](https://photonstorm.github.io/phaser3-docs/index.html). 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](https://www.typescriptlang.org/docs/handbook/type-checking-javascript-files.html) 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](https://github.com/DefinitelyTyped/DefinitelyTyped) (so you need to [manually add them](https://github.com/photonstorm/phaser3-docs/tree/master/typescript) 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!