sequelize: TS typing seems to be broken when using a query with `include: []` option

Issue Description

I cannot seem to get the typing right when trying to find a record and resolve an association, code samples included.

What are you doing?

Here is the link to the SSCCE for this issue: https://github.com/papb/sequelize-sscce/pull/117

What do you expect to happen?

TypeScript happily compiles

What is actually happening?

TypeScript reports the following issue placed at the include object key:

No overload matches this call.
  Overload 1 of 2, '(this: ModelStatic<User>, options: NonNullFindOptions<IUserAttributes>): Promise<User>', gave the following error.
    Type 'typeof Company' is not assignable to type 'Includeable'.
      Type 'typeof Company' is not assignable to type 'typeof Model'.
        Construct signature return types 'Company' and 'Model<TModelAttributes, TCreationAttributes>' are incompatible.
          The types of '_attributes' are incompatible between these types.
            Type 'ICompanyAttributes' is not assignable to type 'TModelAttributes'.
              'ICompanyAttributes' is assignable to the constraint of type 'TModelAttributes', but 'TModelAttributes' could be instantiated with a different subtype of constraint '{}'.
  Overload 2 of 2, '(this: ModelStatic<User>, options?: FindOptions<IUserAttributes>): Promise<User>', gave the following error.
    Type 'typeof Company' is not assignable to type 'Includeable'.
      Type 'typeof Company' is not assignable to type 'typeof Model'.

Additional context

Add any other context or screenshots about the feature request here.

Environment

  • Sequelize version: 6.3.5
  • Node.js version: 14.7
  • Operating System: macOS 10.15.6
  • If TypeScript related: TypeScript version: 3.9

Issue Template Checklist

How does this problem relate to dialects?

  • I think this problem happens regardless of the dialect.
  • I think this problem happens only for the following dialect(s):
  • I don’t know, I was using PUT-YOUR-DIALECT-HERE, with connector library version XXX and database version XXX

Would you be willing to resolve this issue by submitting a Pull Request?

  • Yes, I have the time and I know how to start.
  • Yes, I have the time but I don’t know how to start, I would need guidance.
  • No, I don’t have the time, although I believe I could do it if I had the time…
  • No, I don’t have the time and I wouldn’t even know how to start.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 5
  • Comments: 18 (7 by maintainers)

Most upvoted comments

Thanks, I’ll take a look at this once we get the time for an upgrade attempt. Awesome! ❤️🎉

i’ve got the same issue. thanks to @luis-carlos-campos solution but using association in include options seems to not having the ability of include model using alias ( as ‘xxx’ ) is there any update or fix about it?

I’ve got the same error here.

As a workaround you can create an Association and use it. The main problem is when it comes to nested includes once you must define the model and its include.

Edit: You can achieve nested levels by using associations:

const student = await Student.findByPk(1, {
            transaction: this.transaction,
            include: [
                Student.associations.School,
                {
                    association: Student.associations.Address,
                    include: [
                        {
                            association: Address.associations.City,
                        }
                    ],
                }
            ],
        });