Typing a Truly Empty Object
Now that we've seen the empty object type, let's talk about typing a truly empty object in TypeScript.
Here's a function acceptOnlyEmptyObject
, which is annotated with the empty object:
const acceptOnlyEmptyObject = (input: {}) => {}
However, this doesn't truly restrict the i
Transcript
00:00 While we're thinking about empty objects, though, we probably need to figure out a type for actually what a truly empty object would be. If we look and we say except only empty object function that takes in an empty object,
00:12 this doesn't represent an empty object. It actually represents, as we saw before, strings, numbers, anything with like zero or more properties. But if we actually wanted it to error when we pass properties to this object, how would we handle that?
00:28 You've got a big kind of like load of TS expect errors down here. We're expecting errors really if we pass anything that has a property on it, including null or undefined as well. We're not allowed to pass null or undefined here. So your job is to try to figure out. This is kind of an interesting typing problem.
00:45 I think I'm going to give you some hints below on what you should do here. I think the main one you're going to be interested in is thinking about a record type and thinking about what the properties on that record type could be. Good luck.