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
[Docs] Typescript example breaks automagic field getters #11326
docsFor issues and PRs. Things related to documentation, such as changes in the manuals / API reference.typescriptFor issues and PRs. Things that involve typescript, such as typings and intellisense.
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:
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.
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.
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)!
docsFor issues and PRs. Things related to documentation, such as changes in the manuals / API reference.typescriptFor issues and PRs. Things that involve typescript, such as typings and intellisense.
Activity
papb commentedon Aug 16, 2019
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.
SimonSchick commentedon Aug 16, 2019
Please follow the issue template.
sunwukonga commentedon Aug 19, 2019
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.
SimonSchick commentedon Aug 19, 2019
We should really discourage the use of babel with sequelize, it causes so many issues...
psalz commentedon Aug 20, 2019
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-typescriptplugin runs before@babel/plugin-proposal-class-properties, everything works fine (Babel REPL)!