-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
TypeScript: Type improvements for filterQuery, LeanDocument #11673
Conversation
… add in another PR
…n't break anything
@@ -27,7 +27,7 @@ ParentModel.findOne({}).populate('child').orFail().then((doc: Parent & Document) | |||
} else { | |||
useChildDoc(child); | |||
} | |||
const lean = doc.toObject(); | |||
const lean = doc.toObject<Parent>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do tests fail without this change? I'd prefer if we didn't require devs to add generics to every single toObject()
call
/** | ||
* Helper type for getting a find Query type for a given Model (return of Model.find) | ||
*/ | ||
type QueryForModel<T extends Model<any>> = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does this type do? Doesn't look like it is used
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That was added in #11650 -- there is an open question in the other branch on if you want me to pull it out. I find it to be a useful helper for getting the type of Query but it can be removed
type LoosenType<R> = R extends infer T ? | ||
[T] extends [string] ? T | RegExp : | ||
[T] extends [ObjectIdLike] ? T | ObjectIdLike | { _id: ObjectIdLike | string } | string : | ||
[T] extends [globalThis.Date] ? T | number : |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We do also allow certain strings for dates
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Easily added, I'll do that
Great work, I was always wondering when/if we'll be able to support types casting and still get type safety. |
Realistically, I'm gonna have to close this. We had to revert most of #11650 because of performance issues. |
Summary
This PR enables useful types on FilterQuery -- currently FilterQuery knows field names but not types. It has a "LoosenType" typescript helper which allows automatic mongoose typecasting for things like ObjectId/String, Date/number, etc, and it also filters out functions. I've added a lot of tests to make sure everything is working the way we expect it to.
Note that this PR is based on #11650 and can't be merged separately
Examples
See the updated types/ unit tests