sequelize / sequelize-typescript

Decorators and some other features for sequelize
MIT License
2.78k stars 280 forks source link

typescript type infer cannot work accurately #763

Open dreamerblue opened 4 years ago

dreamerblue commented 4 years ago

Versions

I'm submitting a ...

[x] bug report [ ] feature request

Actual behavior:

Type of attributes and where cannot be inferred by typescript.

And return type of query is always MyModel.

Expected behavior:

Type inference can work.

Steps to reproduce:

Please see code below.

Related code:

user.model.ts

@Table({
  tableName: 'user',
  freezeTableName: true,
  timestamps: false,
})
export default class UserModel extends Model<UserModel> {
  @Column({
    field: 'user_id',
    primaryKey: true,
    autoIncrement: true,
    type: DataType.INTEGER,
  })
  userId!: number;

  @Column({
    field: 'user_name',
    type: DataType.STRING(50),
  })
  username!: string;
}

user.service.ts

async getDetail(userId: number) {
  const res = await this.userModel.findOne({
    attributes: ['userId', 'username', 'wrongField1'], // should have an error for invalid field
    where: {
      userId,
      wrongField2: 'here', // should have an error
    },
    raw: true,
  });
  // type of `res` is `UserModel | null`, excepted plain object `{ userId: number; username: string; } | null`
  return res;
}
romulogrowthtech commented 4 years ago

@dreamerblue the same problem

shamoons commented 4 years ago

Same issue - anyone have anything?

wdzeng commented 3 years ago

Same problem

aperkaz commented 2 years ago

Its there a solution for this?? I am facing the same problem...