-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Excess property checking for instances or other object types #28464
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
So you are looking for exact types? There's already an issue for that |
Exact types: #12936 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: const alex: Person = new Student('alex', '[email protected]', 20) though I agree that this piece just feels inconsistent: const john: Person = {
name: 'john',
email: '[email protected]',
age: 30
} |
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?
|
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. |
Search Terms
object literal class instance
treat object literal as class instance
Suggestion
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 likepasswordHash
. 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.
Examples
Playground link
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: