Skip to content

Excess property checking for instances or other object types #28464

Closed

Description

@Hoishin

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.

Activity

  1. ajafff commented on Nov 10, 2018

    @ajafff
    Contributor

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

  2. DanielRosenwasser commented on Nov 11, 2018

    @DanielRosenwasser
    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
    }
  3. changed the title [-]Treat Class Instance as Object Literal[/-] [+]Excess property checking for instances or other object types[/+] on Nov 11, 2018
  4. Hoishin commented on Nov 12, 2018

    @Hoishin
    Author

    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?

    • check excess property of non-object literal
    • force a value to be object literal
  5. typescript-bot commented on Dec 13, 2018

    @typescript-bot
    Contributor

    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.

  6. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions