In this episode, we dive into how enums can implement interfaces, and why that's a super handy feature! We start out with the basics—imagine you're building a YouTube-like video app, with enums representing stuff like video visibility (public/private) and encoding status (waiting, failed, success).
Both these enums could use a color in the UI—red for failure and private, green for success and public, yellow/orange for waiting, and so on. We show how you can easily implement a color method inside each enum that matches its status to a color string.
But then we notice something: both enums have a color
method! That's where interfaces come in. We create an interface, say HasColor
, which just requires that anything implementing it must have a color(): string
method. Now, both enums can implement this interface, so anywhere in your app, you can deal with any status (encoding, visibility, or others in the future) and be sure there’s a color()
method available.
Throughout the episode, we code out these examples, check the results, and see how enforcing the interface means you can’t forget to add that color method to your enums. In short—you’ll see how using interfaces with enums makes your code more robust and keeps everything consistent!