Repository navigation
Closed
Labels
existing workaroundFor issues. There is a known workaround for this issue.typescriptFor issues and PRs. Things that involve typescript, such as typings and intellisense.
Description
What are you doing?
I'am using Babel for code compilation + TypeScript for type-checking.
Two scenarios:
- Using define, which is "really wouldn't recommend this" (according types/test/define.ts comments)
interface User extends Model {
id: number;
login: string;
}
type UserModel = {
new (): User;
} & typeof Model;
const User = sequelize.define(
'User',
{ login: DataTypes.STRING },
{ tableName: 'user' }
) as UserModel;- Using new approach (based on code, found in the types/models/User.ts)
export class User extends Model {
public id: number;
public login: string;
}
User = User.init(
{ login: DataTypes.STRING },
{ sequelize, tableName: 'user' }
);In DB (Postgres) I have one row with login 'admin'.
For the test, I use this function.
async function test() {
const user = (await User.findOne()) as User;
console.log('login', user.login, user.get('login'));
}
test();What do you expect to happen?
It is logical that in both scenarios I should get
login admin admin
What is actually happening?
However, if in "modern", second approach I see
login undefined admin
And if I print console.log(user) I see
user1 User {
dataValues:
{ id: 1,
login: 'admin',
createdAt: 2019-03-18T23:00:33.516Z,
updatedAt: 2019-03-18T23:00:33.516Z },
_previousDataValues:
{ id: 1,
login: 'admin',
createdAt: 2019-03-18T23:00:33.516Z,
updatedAt: 2019-03-18T23:00:33.516Z },
_changed: {},
_modelOptions:
{ timestamps: true,
validate: {},
freezeTableName: false,
underscored: false,
paranoid: false,
rejectOnEmpty: false,
whereCollection: null,
schema: null,
schemaDelimiter: '',
defaultScope: {},
scopes: {},
indexes: [],
name: { plural: 'Users', singular: 'User' },
omitNull: false,
sequelize:
Sequelize {
options: [Object],
config: [Object],
dialect: [PostgresDialect],
queryInterface: [QueryInterface],
models: [Object],
modelManager: [ModelManager],
connectionManager: [ConnectionManager],
importCache: {},
test: [Object] },
tableName: 'user',
hooks: {} },
_options:
{ isNewRecord: false,
_schema: null,
_schemaDelimiter: '',
raw: true,
attributes: [ 'id', 'login', 'createdAt', 'updatedAt' ] },
isNewRecord: false,
id: undefined,
login: undefined }Dialect: postgres
"sequelize": "^5.1.0",
upd:
Just tested with ts-node - everything works as it should.
Here is my .babelrc
{
"sourceMaps": "inline",
"retainLines": true,
"presets": [
[
"@babel/preset-env",
{
"targets": {
"node": true
}
}
],
"@babel/preset-typescript"
],
"plugins": [
"@babel/plugin-proposal-optional-chaining",
"@babel/plugin-proposal-object-rest-spread",
"@babel/proposal-class-properties"
]
}Reacted by Lei Wang, Andrew Keller, Tom Moor and jony89
Metadata
Metadata
Assignees
Labels
existing workaroundFor issues. There is a known workaround for this issue.typescriptFor issues and PRs. Things that involve typescript, such as typings and intellisense.
Activity
SimonSchick commentedon Mar 20, 2019
Can you please post the code that is generated by babel for the user model?
I have the suspicion it emits an
this.login = undefinedassignment because when I test the same code (in mysql) you provided in plain JS it works fine.fix(typescript): do not use const enums (#10585)
DanielRamosAcosta commentedon Apr 24, 2019
I'm having the same problem @petrovi4, how did you solved it?
DanielRamosAcosta commentedon Apr 24, 2019
Here is the generated code by babel with the typescript preset:
I believe the problem is that the super call sets the fields, but then they're setted to
undefinedby the_definePropertyfunction :/DanielRamosAcosta commentedon Apr 24, 2019
It's a problem generated by the
babel-plugin-proposal-class-properties. Do you know any what to fix this @SimonSchick? Maybe we can call a setup method from the constructor.DanielRamosAcosta commentedon Apr 24, 2019
@SimonSchick It might be useful to call the
_initValuesfrom the child class?DanielRamosAcosta commentedon Apr 24, 2019
The only other solution I see, is remove the
babel-plugin-proposal-class-propertiesand upgrade to node 12 which supports class properties natively 😕Telokis commentedon May 10, 2019
@DanielRamosAcosta Did you manage to make it work? Babel complains that
babel-plugin-proposal-class-propertiesis not found even though I am using Node 12.DanielRamosAcosta commentedon May 11, 2019
@Telokis I wasn't using
staticnor class properties that much, so I ended removing thebabel-plugin-proposal-class-properties... bad solution 😢Telokis commentedon May 11, 2019
@DanielRamosAcosta Thanks for the answer
aaahrens commentedon Oct 15, 2019
Any solution to this by any chance? I am running this with nextjs and it makes a lot of boilerplate ; ;
Telokis commentedon Oct 15, 2019
@aaahrens If I remember correctly, a solution was to do it this way:
Even though it's not pretty.
22 remaining items
swarthy commentedon Jun 21, 2021
Same issue with raw Typescript example starting version 4.3, because now
tscgenerates empty fieldsinstead of
EtienneK commentedon Jun 28, 2021
Experiencing the same issue. Please reopen.
dfyz011 commentedon Aug 2, 2021
Please reopen, I still don't see any normal solution
DanielRamosAcosta commentedon Aug 2, 2021
For me, the solution has been stop using sequelize, and change to MikroORM
rudymalhi commentedon Aug 16, 2021
The solution for me was to set
"target": "ES2020"intsnconfig.json(it was set to ESNEXT)edrpls commentedon Aug 27, 2021
This is not really a solution, but dropping babel in favor of esbuild fixed this for me, plus, it's less configuration than babel+webpack.
ortonomy commentedon Sep 22, 2021
@pieczorx can you tell me how you came up with this?
Problem here:
_initValuesis not a valid property on the Model type and the typescript compiler complains.ortonomy commentedon Sep 22, 2021
Why was this closed? It's September 2021 and I'm using Nuxt and despite following exactly the typescript setup in Sequelize, ALL of the fields in the class are undefined and lazy loading associations does not work. All broken...
intrinety commentedon Oct 24, 2021
I have the same problem when use class definition.
I resolve this issue by change to the default way of defining a model. It older but works.
I have tested for feature Transaction and Association still work. this is my solution.
Nextjs version: 11.1.2
Sequelize: 6.6.5
Typescript: 4.4.4
ephys commentedon Dec 29, 2021
I'm reopening to document the workaround (see #10579 (comment)).
Note that this is regarding the issue with babel. If you're experiencing the issue with a different transpiler, please open another bug report with all the necessary information.
ephys commentedon Jan 2, 2022
Closed by #13877
eungook commentedon Nov 18, 2022
try esbuild instead of babel. it works for me.