sequelize-typescript icon indicating copy to clipboard operation
sequelize-typescript copied to clipboard

typescript type infer cannot work accurately

Open dreamerblue opened this issue 4 years ago • 4 comments

Versions

  • sequelize: 5.21.5
  • sequelize-typescript: 1.1.0
  • typescript: 3.8.3
  • @types/bluebird: 3.5.30

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;
}

dreamerblue avatar Apr 06 '20 04:04 dreamerblue

@dreamerblue the same problem

romulogrowthtech avatar Apr 06 '20 15:04 romulogrowthtech

Same issue - anyone have anything?

shamoons avatar May 04 '20 18:05 shamoons

Same problem

wdzeng avatar Jan 12 '21 15:01 wdzeng

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

aperkaz avatar Nov 05 '21 13:11 aperkaz