You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Excess property checking for instances or other object types #28464
According to this part of documentation, object literal is type-checked more strictly than other objects for excess properties. It would be nice to have feature to check excess properties for other "non-literal" objects such as class instances.
Use Cases
Let's say we have data fetched from database. It would include createdAt, updatedAt, or something like passwordHash. Some properties are not the best to get out of server.
When sending the data to client, I defined the payload type to not include those excess properties, but if the data is an form of class instance, the excess properties are allowed.
We don't have excess property checking on other object types because excess property checks are mostly meant to catch issues where an object literal is supposed to be used as a "bag of options".
To try to avoid other issues, we also have overlap checking for "weak types", where if there's no overlap between a Source type to a Target type, and the target type only has optional properties (i.e. it's "weak") then we error if there's no overlap.
But here, it's not even clear why this example should be an error:
I have seen Exact types, but I wasn't sure if that was something I was looking for, but after looking back, it seems that they should result in what I want to see.
What would be the current best way to do either of these in order to deal with the use cases?
Activity
ajafff commentedon Nov 10, 2018
So you are looking for exact types? There's already an issue for that
DanielRosenwasser commentedon Nov 11, 2018
Exact types: #12936
Nominal types: #202
We don't have excess property checking on other object types because excess property checks are mostly meant to catch issues where an object literal is supposed to be used as a "bag of options".
To try to avoid other issues, we also have overlap checking for "weak types", where if there's no overlap between a Source type to a Target type, and the target type only has optional properties (i.e. it's "weak") then we error if there's no overlap.
But here, it's not even clear why this example should be an error:
though I agree that this piece just feels inconsistent:
[-]Treat Class Instance as Object Literal[/-][+]Excess property checking for instances or other object types[/+]Hoishin commentedon Nov 12, 2018
Klaus Meinhardt (@ajafff) Daniel Rosenwasser (@DanielRosenwasser)
I have seen Exact types, but I wasn't sure if that was something I was looking for, but after looking back, it seems that they should result in what I want to see.
What would be the current best way to do either of these in order to deal with the use cases?
typescript-bot commentedon Dec 13, 2018
This issue has been marked as a duplicate and has seen no activity in the last day. It has been closed for automatic house-keeping purposes.