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

No type inference for 'create' association ?

Open TheCelebrimbor opened this issue 3 years ago • 3 comments

Suppose I have a model

class user extends Model<... >
{
  // User hasOne UserCredentials via idUser
  userCredential!: UserCredentials;
  getUserCredential!: Sequelize.HasOneGetAssociationMixin<UserCredentials>;
  setUserCredential!: Sequelize.HasOneSetAssociationMixin<UserCredentials, UserCredentialsId>;
  createUserCredential!: Sequelize.HasOneCreateAssociationMixin<UserCredentialsCreationAttributes>;
}

The createUserCredential does not have type inferences for its properties.

export type HasOneCreateAssociationMixin<TModel> = (
  values?: { [attribute: string]: unknown },
  options?: HasOneCreateAssociationMixinOptions
) => Promise<TModel>;

Shouldn't the values type infer from the UserCredential class instead of being unknown ?

Also if I make a call like user.createUserCredential({}). What should the keys be? the properties of the model or the column names in the DB ?

TheCelebrimbor avatar Jun 04 '21 06:06 TheCelebrimbor

Hmm. I thought that declaring createUserCredential as

Sequelize.HasOneCreateAssociationMixin<UserCredentialsCreationAttributes>

would somehow cause the values parameter to be the appropriate creation type, i.e. UserCredentialsCreationAttributes

steveschmitt avatar Jun 04 '21 18:06 steveschmitt

@steveschmitt Is there anything I can do about it for now ? because all this is autogen, I am skeptical to modify any generated-stuff myself. But this issue is rather annoying as there is no member-inference. should the values type be defined as UserCredentialsCreationAttributes instead of { [attribute: string]: unknown } ?. If so I can change it manually now.

Is this a bug that will be fixed ?

TheCelebrimbor avatar Jun 10 '21 12:06 TheCelebrimbor

As far as this link goes : https://sequelize.org/master/class/lib/associations/has-one.js~HasOne.html

The type of the values seems to be Object by default. Is there where the issue lies perhaps ? image

TheCelebrimbor avatar Jun 10 '21 12:06 TheCelebrimbor