How can you create a TypeScript object type which cannot have a property at all? Learn the semantics of combining optional property and the never
type.
Transcript
Let's say that you find yourself in a situation when you have an object type and you would like to restrict that a certain property cannot exist. For this reason let's create an OBJ type which would have a property whatever that is and whatever value it will hold and let's create an example obj of object type and let's instantiate it. The property is going to have some example value just to have a reference object. The first thing that could come to your mind is actually to create an optional property. Whereas it's absolutely valid to create an object which doesn't have the property, the thing is we would not like to have it that we could have the property or not, both are valid in this case, but we would like this not to be able to create the property at all.
So we would like this to throw a compilation error. So let's try another attempt. Something that you could know is that the never type comes very handy when you want to create something that could never exist because a never is a type which have no proper values apart from other never but the whole point here is that you cannot instantiate a never in any meaningful way. So the problem in this approach is that if you create a property which has the never type, then TypeScript interprets this in a way that you do have an object type, so first of all that has to be an object, and you have a required property, which is called property in this case, so you have to create it but there is no valid way to instantiate it. So what we need to do is actually to combine the two approaches and in this way if the property does not exist at all, this is fine because this property could not exist at all but if you try to create it then the issue is that there is no valid way to create it at all.
So whatever we put here is going to be invalid unless we just delete and remove the property.