Skip to content

Sequelize 7 + TS: type-checking fails on query if the where clause includes null for a nullable attribute #17401

@RJFelix

Description

@RJFelix

Issue Creation Checklist

  • I understand that my issue will be automatically closed if I don't fill in the requested information
  • I have read the contribution guidelines

Bug Description

TypeScript inferred types: findAll incorrectly fails type-checking when using null as the value for a nullable field in the where clause.

If we skip type-checking, the query runs fine and returns the expected result.

Reproducible Example

Here is the link to the SSCCE for this issue: sequelize/sequelize-sscce#278

This is a type-checking issue and I'm not sure how to write a unit test asserting that a term type-checks, so I expect the SSCCE to pass testing with Postgres.

What do you expect to happen?

Given a model with a nullable array attribute:

class Foo extends Model<InferAttributes<Foo>, InferCreationAttributes<Foo>> {
  // ...
  
  @Attribute(DataTypes.ARRAY(DataTypes.STRING))
  declare colors?: Array<string>
}

Then null should be included in that attribute's type for the where-clause of a query.

// Works fine, doesn't type-check
const result = Foo.findAll({
  where: {
    colors: null
  }
})

What is actually happening?

The findAll call returns the correct results, but fails type checking because the type of where.colors does not include null:

No overload matches this call.
  Overload 1 of 2, '(this: ModelStatic<Foo>, options: Omit<FindOptions<InferAttributes<Foo, { omit: never; }>>, "raw"> & { raw: true; }): Promise<...>', gave the following error.
    Type '{ colors: null; }' is not assignable to type 'WhereOptions<InferAttributes<Foo, { omit: never; }>>'.
      Types of property 'colors' are incompatible.
        Type 'null' is not assignable to type 'WhereAttributeHashValue<string[] | undefined>'.
  Overload 2 of 2, '(this: ModelStatic<Foo>, options?: FindOptions<InferAttributes<Foo, { omit: never; }>> | undefined): Promise<...>', gave the following error.
    Type '{ colors: null; }' is not assignable to type 'WhereOptions<InferAttributes<Foo, { omit: never; }>>'.
      Types of property 'colors' are incompatible.
        Type 'null' is not assignable to type 'WhereAttributeHashValue<string[] | undefined>'. ts(2769)
  model.d.ts(104, 3): The expected type comes from property 'where' which is declared here on type 'Omit<FindOptions<InferAttributes<Foo, { omit: never; }>>, "raw"> & { raw: true; }'

Environment

  • Sequelize version: @sequelize/core 7.0.0-alpha.41
  • Node.js version: v20.14.0
  • If TypeScript related: TypeScript version: typescript 5.4.5
  • Database & Version: PostgreSQL 16
  • Connector library & Version: pg 8.12.0

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 will need guidance. Mostly just a nudge in the right direction for how the WhereAttributeHashValue type is constructed.
  • No, I don't have the time, but my company or I are supporting Sequelize through donations on OpenCollective.
  • No, I don't have the time, and I understand that I will need to wait until someone from the community or maintainers is interested in resolving my issue.

Indicate your interest in the resolution of this issue by adding the 👍 reaction. Comments such as "+1" will be removed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    pending-approvalBug reports that have not been verified yet, or feature requests that have not been accepted yet

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions