Nicolas Dominguez
Nicolas Dominguez

Reputation: 1343

Typescript incorrect interface for sequelize.Model.Update?

I'm working with Sequelize and typescript. I'm using this definition sequelize.d.ts

I found a problem using the model.update method, this interface receive as first argument the values that will be updated, but this argument has a TAttributes Type. I think this argument should be of string type. because we can use this function by sending only a subset of TAttributes.

Let me put an example, if I have a model like this:

 Person.attribues: {
   id: number;
   field1: string;
   field2: number;
 }

and then, after create and persist an instance, I want update filed2, I should do something like this:

Person.update(
   {field2: 5},
   {where: {id: 1}}
)

for this example, I'm having an typescript error because the first argument expect something of type Person.attributes and I'm providing only the subset {field2: 5}

I already did a question in github, what do you think about that? I'm thinking ok?

Upvotes: 1

Views: 3098

Answers (1)

Ivan Drinchev
Ivan Drinchev

Reputation: 19581

You should use the attributes declaration of sequelize.d.ts with optional property names, exactly because of that.

In your case you will need to do :

interface PersonAttributes {
   id?: number;
   field1?: string;
   field2?: number;
}

By doing this, when you use update it will specifically allow you to update only one property.

You can check the examples in sequelize-tests.d.ts for further reference.

Upvotes: 3

Related Questions