How to make TypeScript infer types in switch statements without casting (in a Redux reducer or anywhere else)

Published on 2022/2/1 by ilDon

As a developer with very poor memory, I really like coding in typed languages. They allow me to forget my data types, and be sure to pass the right props around my code. If you are unfamiliar with typed languages, I suggest you read this book for a general overview, or this article for a more specific hands-on guide for TypeScript. One annoying thing about developing in TypeScript is that it is not obvious how to infer types in switch statements. This is because the compiler doesn't know what type we are passing to the switch statement. I will explain my solution to this problem by example, posting here the relevant part of the code needed to achieve the desired result. I have also set up a live (working) example of the code in this StackBiz project. Because I first developed this while working on a Redux reducer, I will provide an example that imitates what I would do in a Redux reducer. Since it is a simple use-case, it can easily be applied to any other scenario in which you need to infer types in switch statements. I will focus on the how-to part, not the why-it-works part. I came across this solution after I had already written a switch statement in TypeScript. I was not trying to make TypeScript infer types, I was only being as precise as possible in my typing. Turns out, the compiler picked up more than I expected.

SQLite in a PWA with FileSystemAccessAPI

Published on 2021/12/1 by ilDon

Anita now supports SQLite, which means you can save your data in a local database on your device from a Progressive Web App. This is achieved by using the FileSystemAccessAPI, so the user only needs to choose where to keep the database once, and then the PWA can perform all CRUD operations it needs, with some caveats. Before we proceed further in understanding how this works, let's first clear the air about the reason for implementing SQLite in a PWA with the FileSystemAccessAPI. SQLite is a database that is used to store data in a local file system, and the FileSystemAccessAPI is a way to access that file system. So combining the two a PWA can access the database and perform CRUD operations on it. And because Anita is an offline first only app, that is a very good match. An even better reason, though, is well described by paraphrasing a quote from Michael Crichton's Jurassic Park:
I was so preoccupied over whether or not I could, I didn't stop to think if I should.

Porting Anita from Angular to React resulted in 20% less code

Published on 2021/11/10 by ilDon

I started developing Anita in Angular because I had previously developed with it a form generator that could power the core parts of the app. Having years of experience also with React, though, I soon realized that Angular was slowing me down. So I decided to port Anita from Angular to React. The whole project took under 10 days, in which I worked on it only in my spare time. I can easily say that it was well worth it as the code base, with the exact same functionalities, is now 20% smaller.

The journey of Anita to the ultimate bootstrapping thanks to GitHub Pages

Published on 2021/11/2 by ilDon

Since I've started developing for the web (spoiler alert: it has been a couple of decades), I have always looked for ways to develop, host, and distribute my free and open-source projects (FOSS) at no cost. The reasoning is that there are already enough factors that make open source challenging to maintain, so economic sustainability should not be one. My latest FOSS project is Anita, an app that I developed primarily because I needed it, and because the way I want to build it makes it a very fun project to work on. The first requirement that make this project fun is that I want it to cost zero (as in zero kelvins), not considering my labor. I’ll document here on the Anita Blog my experience in developing the most fun parts of Anita. In this first post I explain how I solved the first requirement (zero costs) by developing Anita as a Progressive Web App (PWA), and why I think this approach can suit many other FOSS projects. Due to the nature of this topic, there won’t be much code involved, I’ll improve that in future posts!