Pre-processing my TypeScript project using š© Gents
In my spare time (whatever thatās supposed to mean) Iāve been iterating slowly on a web game. Itās neat, using web technologies like Angular, PWAs, and serverless to make the deployment process straightforward.
Adding Angular is somewhat recent. Rewriting the gameās frontend to TypeScript has actually been really useful in ensuring the game is reliable with each release.
Because admittedly Iām not a very good programmer. I make a lot of mistakes, and in a hobby context Iām really not putting in enough time on writing high-quality tests with fantastic coverage. I lean very heavily on TypeScript to ensure what Iām writing makes sense.
As this game has evolved, I have run into some issues at scale. Typos can happen, and I donāt always think to change everything consistently. This means players have run into null-pointer exceptions or places where the code is just inconsistent.
Over time, Iāve organically moved to improving my development workflow to improve type coverage ahead of time so that I never face one of these mistakes again. Itās a work in progress, but I do want to share one outcome of this project called š© Gents.

Item Management
Hereās one example. In this game, players can collect items. Each item is represented by a key, which is a string. I have a map which lets me grab more details about that item.
Okay, now I want to be able to use the recovery
field by healing characters. How do I ensure that the selection
parameter is a valid item?
Now letās say I also want to distribute items as prizes after certain gameplay events. How do I ensure that the prize in the array is actually a valid item rather than a typo? I may accidentally write āpotinā and suddenly players will have invalid items in their inventory until I discover it.
TypeScript has been useful in allowing me to develop types which can minimize the list of possibilities. If the item key isnāt one of a few instances, the compiler will throw an error until I fix it.
However, how do I convert the keys of a map into a type? TypeScript doesnāt really let you do this. I could maintain a separate list of item types thatās used as the input to the map, but that starts to add complexity to a project that I donāt really have a lot of time to manage.
So I ended up developing a type pre-processor called š© Gents, and it can be downloaded from NPM now.
Creating a gents file
The way this works is very simple. You start with a .gents
file. This is a Node.js script that you will use to programmatically generate your files. Basically youāre just running some code and printing the expected output with console.log
.
This is very simple, and thatās all it needs to be. Iām not looking for anything too complicated, just a way to convert my items map into a type.
When I run the command line script, it will scan my directory for all .gents
files and convert them to a .ts
file of the same name.
Now when I go back to battle-rewards.ts file, I can update the type away from string
and to something more specific. Now the TypeScript compiler is able to catch my typo and ensure that things are just a bit safer.
Wrapping Up
This is just the v1.0.0 release. Now you may be asking whatās next. Nothing. Honestly Iām happy with the project at this point and I donāt really see anything else that needs to be done.
But if you have any ideas, add an issue or file a pull request on the GitHub page.