Categories
apple ios

Advanced TypeScript: How to Use Interface Inheritance With Discriminated Unions

What you want to do when your occasions lengthen every different

Jim Redfern

Recently, I had to create sorts for a JavaScript match listener that may be utilized in a TypeScript mission. There had been a couple of occasions that prolonged every different — kid occasions contained the houses of mother or father occasions, and match houses had to be accessed inside the listener.

The evident resolution used to be to make use of interface inheritance with a union kind. I searched for methods to do that the use of loyal assets —The TypeScript Handbook, Stack Overflow, and Medium — however, I couldn’t discover a description of how one can make it paintings. I had to transfer ahead with this means for the reason that handiest different obvious resolution sucked: duplicating match houses 🤢.

So, I made a toy instance of the issue, talked it via with my teammates at Tradeshift, and between us got here up with an answer 😎.

This is the object that I’d was hoping to seek out.