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

Cannot use FilterQuery<Type> when Type is a class #14473

Closed
2 tasks done
giavy0209 opened this issue Apr 1, 2024 · 3 comments · Fixed by #14510
Closed
2 tasks done

Cannot use FilterQuery<Type> when Type is a class #14473

giavy0209 opened this issue Apr 1, 2024 · 3 comments · Fixed by #14510
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@giavy0209
Copy link

giavy0209 commented Apr 1, 2024

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

8.2.3

Node.js version

20

MongoDB server version

7

Typescript version (if applicable)

^5

Description

When I create a generic function that receive a generic type D and D extends from AbstractSchema then I could not use FilterQuery<D> with any type

Steps to Reproduce

export class AbstractSchema {
  _id: any
  createdAt: Date
  updatedAt: Date
  deletedAt: Date 
}
export const generateExists = <D extends AbstractSchema = AbstractSchema>() => {
  const query : FilterQuery<D>= {deletedAt : {$ne : null}} // or {deletedAt : new Date()}
  //Typescript show error at query Type 'null' is not assignable to type 'ApplyBasicQueryCasting<QueryTypeCasting<D["deletedAt"]>> | undefined'
}

Expected Behavior

No response

@orgads
Copy link
Contributor

orgads commented Apr 1, 2024

Looks like a dup of #14459 / #14462.

@vkarpov15 vkarpov15 added this to the 8.2.5 milestone Apr 4, 2024
@vkarpov15 vkarpov15 added the typescript Types or Types-test related issue / Pull Request label Apr 4, 2024
@Marchelune
Copy link

Hi, I also encountered the issue in 8.3.0, but I don't think it's linked to classes, I have the same with a type:

export function setCreatedOnInFilter<Entity extends { createdOn: Date }>(
    mongooseFilter: FilterQuery<Entity>,
    someDate: Date,
) {
    mongooseFilter.createdOn = { $gt: someDate };
}

This yields the compile error (typescript strict mode on):

Type '{ $gt: Date; }' is not assignable to type 'Condition<Entity["createdOn"]> | undefined'.
  Types of property '$gt' are incompatible.
    Type 'Date' is not assignable to type 'ApplyBasicQueryCasting<QueryTypeCasting<Entity["createdOn"]>> | undefined'.
      Type 'Date' is missing the following properties from type 'QueryTypeCasting<Entity["createdOn"]>[]': length, pop, push, concat, and 29 more. ts(2322)

As hinted in this comment, it works in 8.2.2. TBH I'm not sure I understand how TS resolves the type condition to an array.

@NeoKms
Copy link

NeoKms commented Apr 10, 2024

Same problem. In version 8.2.3 you broke backward compatibility.

vkarpov15 added a commit that referenced this issue Apr 10, 2024
types(query): make `FilterQuery` props resolve to `any` for generics support
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
typescript Types or Types-test related issue / Pull Request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants