Skip to content

[Docs] Typescript example breaks automagic field getters #11326

Closed

Description

@psalz

I followed the documentation on how to set up a model with Typescript and ran into an issue: If fields are declared on the model class as described here, they will be initialized to undefined upon instantiation and overwrite the automagic getters generated by Sequelize. I.e., if following the documentation, the console.log(newUser.id, newUser.name, newUser.preferredName); at the bottom will actually print <some id> undefined undefined.

I'm currently working around this by deleting all of the undefined properties on an instance, so that any field lookup will fall back to the prototype's automagic getters:

  MyModel.addHook('afterCreate', (instance, options) => {
    for (const [prop, desc] of Object.entries(
      Object.getOwnPropertyDescriptors(instance)
    )) {
      if (
        desc.get === undefined &&
        desc.set === undefined &&
        desc.value === undefined
      ) {
        delete (instance as any)[prop]
      }
    }
  })

I'm wondering if something like this should be done by Sequelize itself, perhaps enabled through an option on Model.init? In any case, the documentation should be updated to properly handle this.

Environment

Sequelize version: 5.15.0
Node Version: 10.15.0
TypeScript version: 3.2.2

Activity

  1. papb commented on Aug 16, 2019

    @papb
    Member

    Hello, thanks for the detailed report. I am not familiar with the TypeScript usage of Sequelize, so let's wait for @SimonSchick to take a look later.

  2. added
    docs
    typescript
    on Aug 16, 2019
  3. SimonSchick commented on Aug 16, 2019

    @SimonSchick
    Contributor

    Please follow the issue template.

  4. added
    status: awaiting response
    and removed on Aug 16, 2019
  5. sunwukonga commented on Aug 19, 2019

    @sunwukonga

    I had the same issue and opened a question on SE.

    Long story short, it was a babel plugin "@babel/proposal-class-properties". Which causes the model fields to be defined/initialized on the model as undefined.

    Removing the plugin should clear it up as it did for me.

  6. SimonSchick commented on Aug 19, 2019

    @SimonSchick
    Contributor

    We should really discourage the use of babel with sequelize, it causes so many issues...

  7. removed
    status: awaiting response
    on Aug 19, 2019
  8. psalz commented on Aug 20, 2019

    @psalz
    Author

    Thanks @sunwukonga , that indeed appears to be the culprit. I did some more testing and it turns out that if you ensure that the @babel/plugin-transform-typescript plugin runs before @babel/plugin-proposal-class-properties, everything works fine (Babel REPL)!

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

    docstypescript

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions