Skip to content

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

Closed
5 tasks done
Hoishin opened this issue Nov 10, 2018 · 4 comments
Closed
5 tasks done

Excess property checking for instances or other object types #28464

Hoishin opened this issue Nov 10, 2018 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@Hoishin
Copy link

Hoishin commented Nov 10, 2018

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 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.

Examples

Playground link

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
    • If this feature is introduced as additional feature
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.
@ajafff
Copy link
Contributor

ajafff commented Nov 10, 2018

So you are looking for exact types? There's already an issue for that

@DanielRosenwasser
Copy link
Member

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:

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
}

@DanielRosenwasser DanielRosenwasser added the Duplicate An existing issue was already created label Nov 11, 2018
@DanielRosenwasser DanielRosenwasser changed the title Treat Class Instance as Object Literal Excess property checking for instances or other object types Nov 11, 2018
@Hoishin
Copy link
Author

Hoishin commented Nov 12, 2018

@ajafff @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?

  • check excess property of non-object literal
  • force a value to be object literal

@typescript-bot
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants