Handling a Truly Empty Object in TypeScript
There are a couple of ways to solve this problem.
Solution 1: Use a Record
The first approach is to use the Record
type helper to specify the type.
In this case, the key of the Record will be PropertyKey
. Recall that the PropertyKey
says that the key will be a string, number, or symbol.
Transcript
00:00 Okay, so there are actually two solutions here. The first solution that comes to my mind, at least, is to make this a record. Okay, so record being like basically a type helper that we can use to create object types. And then the key of the record is going to be property key. We've seen this in a previous exercise.
00:18 Property key basically says this is gonna be string or number or symbol. If we take a look at it, string or number or symbol. Nice, nice global type available to us. And then the value on this is the key. So if we add number onto here, then this is gonna work for most cases except for the one where we actually pass a number to it. We add string, same.
00:37 It's gonna disallow most things except for when we actually pass a string here. Yeah, so if we change this to a string, then we're gonna see this error sourced. So how do we say we don't want to ever have anything on the property value here? Well, we just say never. So this is a nice solution.
00:58 It basically says, okay, you can only pass an empty object. And whenever you try to add anything, then it's going to say type string is not assignable to type never. That's quite a descriptive thing. It sort of gives us a sense for, and if we alias this, for instance, to like empty object type. So let me do that quickly. Extract type alias, empty object.
01:18 Then that gives us a nice kind of indicator as to what this is supposed to be. Oddly enough, there's been some discussion online and the best way to do this is this funky piece of syntax. So this, I don't want to dive into the discussion properly because this sort of very rarely comes up,
01:35 but in the library type fest, which is by Sindre Sorhus, sorry if I'm getting your name wrong, who is a extremely prolific open source person, then this type empty object here is basically saying, okay, we create a tag, which is a unique symbol.
01:52 And then we use that tag inside the object type definition. And then on that, we say never. This has exactly the same behavior, but it has a few more edge cases covered. So I think my main solution here with record property key never, this is going to sort you out in most cases. But if you want to deep dive and find a slightly more complex
02:11 and slightly more edge case resistant solution, then you can check out the discussion, which I've linked above. And also as well, you can use type fest if you want to just have an empty object type, which you can import from a third party library without needing to explain your code yourself. So type fest is a nice way that you can basically grab types
02:30 that have been pre-made by other people for various use cases and have them all nicely documented. So a truly empty object type, this is probably the most correct solution, but in 95% of cases, the record property key never is also going to sort you out.