Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

order property not assignable to FindOptions if stored in a variable #1697

Open
1 of 2 tasks
Tchekda opened this issue Oct 18, 2023 · 0 comments
Open
1 of 2 tasks

order property not assignable to FindOptions if stored in a variable #1697

Tchekda opened this issue Oct 18, 2023 · 0 comments

Comments

@Tchekda
Copy link

Tchekda commented Oct 18, 2023

Issue

When passing FindOptions through a variable instead of defining them in the function call, typescript throws an error.

Versions

  • sequelize: 6.32.1
  • sequelize-typescript: 2.1.5
  • typescript: 4.9.5

Issue type

  • bug report
  • feature request

Actual behavior

When passing FindOptions directly to findAll, findByPk, fondOne, ..., everything works as expected:

return MyModel.findAll({
  where: { modelId: id},
  attributes: {
    exclude: ['created_at', 'updated_at', 'context']
  },
  include: [ /* Multiple includes */ ],
  order: [
    ['COL_X', 'DESC'],
    [sequelize.col('`INCLUDE_1`.`order`'), 'ASC'],
    [sequelize.col('`INCLUDE_2`.`order`'), 'ASC'],
    [sequelize.col('`INCLUDE_3`.`order`'), 'ASC']
  ],
  subQuery: false,
})

But when I pass those options as a spread variable, I get this :

// Meant to be reused across multiple calls of .findAll, .findOne, .findByPk
const QUERY = {
  where: { modelId: id},
  attributes: {
    exclude: ['created_at', 'updated_at', 'context']
  },
  include: [ /* Multiple includes */ ],
  order: [
    ['COL_X', 'DESC'],
    [sequelize.col('`INCLUDE_1`.`order`'), 'ASC'],
    [sequelize.col('`INCLUDE_2`.`order`'), 'ASC'],
    [sequelize.col('`INCLUDE_3`.`order`'), 'ASC']
  ],
  subQuery: false,
};

return MyModel.findAll(QUERY);
Argument of type '{ attributes: { exclude: string[]; }; include: ({ model: typeof INCLUDE_MODEL_1; as: string; required: boolean; attributes: string[]; through: { attributes: any[]; }; } | { model: typeof INCLUDE_MODEL_2; as: string; required: boolean; attributes: string[]; through: { ...; }; } | { ...; } | { ...; })[]; ord...' is not assignable to parameter of type 'FindOptions<MyModelAttributes>'.
  Types of property 'order' are incompatible.
    Type '(string | Col)[][]' is not assignable to type 'Order'.
      Type '(string | Col)[][]' is not assignable to type 'OrderItem[]'.
        Type '(string | Col)[]' is not assignable to type 'OrderItem'.
          Type '(string | Col)[]' is not assignable to type '[OrderItemAssociation, OrderItemAssociation, OrderItemAssociation, OrderItemAssociation, OrderItemColumn, string]'.
            Target requires 6 element(s) but source may have fewer.

For now I have to add @ts-ignore above the function call to silent those errors, and the queries work perfectly as they match the sequelize expected format

Expected behavior

Have the typing work the same way of the options are passed directly or through a variable

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant