-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
types: stricter projection typing with 1-level deep nesting #15418
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
Conversation
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.
Pull Request Overview
This PR refines projection typing by enforcing stricter type constraints for 1-level deep nested projection paths while maintaining compatibility with Typegoose.
- Enforces that projection values are strictly boolean or 0/1.
- Adds comprehensive tsd tests to validate valid and invalid projection use cases.
- Supports manual casting via ProjectionType for unknown projection fields.
Sure. I will also take a look at the original PR + Typegoose this week. BTW. On this branch, I do not get any type suggestions for projection. |
@pshaddel which IDE are you using and can you try restarting? Type suggestions for projections seem to work well for me on this branch in zed@0.181.8 |
You are right, it needed a restart. These methods from here are also included: child?: PopulatedDoc<HydratedDocument<Child>> I have a draft on my PR trying to remove these methods: /**
* Extract whatever is inside the PopulatedDoc
* @example
* type Doc = { name: string };
* type Sample = PopulatedDoc<Doc>;
* type DepopulatedDoc = DepopulatedDoc<Sample>; // Result: Doc
*/
export type DepopulatedDoc<T> = T extends PopulatedDoc<infer U, infer RawID> ? Exclude<U, RawID> : T;
/**
* Extract the raw document from a hydrated document
* @example
* type Doc = { name: string };
* type Sample = HydratedDocument<Doc>;
* type ExtractedDoc = DehydrateDocument<Sample>; // Result: Doc
*/
export type DehydrateDocument<T> = T extends HydratedDocument<infer U> ? U : T; |
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.
Can confirm this works with current typegoose types, thanks.
Co-authored-by: hasezoey <hasezoey@gmail.com>
@vkarpov15 I am not able to do something like this even though it should be allowed: const currentWeekDoc = await this.currentWeekModel
.findOne(
{ _id: currentWeekId },
{
_id: 0, // <----- error here because it should be allowed
season: 1,
seasonType: 1,
week: 1,
timestamp: 1,
},
)
.lean()
.exec(); |
@msalafia it is happening also into my project, but I found out the reason. I had to do the following change to remove that error: -const userSchema = new Schema<IUser>({
+const userSchema = new Schema({ and -const User = model<IUser>('User', userSchema);
+const User = model('User', userSchema); or |
@DavideViolante this looks like a workaround. Passing an interface is still a valid option in mongose (see here). Since i am using nestjs/mongoose and not mongoose directly, it's library itself that creates Schema for me (and does it with interfaces),, thus i cannot use your suggestion. Anyway i still think there is something broken in the new type definition of ProjectionType. |
@msalafia we will have a fix in 8.15.1. As a workaround in the interim, you can do the following: import { ProjectionType } from 'mongoose';
const currentWeekDoc = await this.currentWeekModel
.findOne(
{ _id: currentWeekId },
{
_id: 0,
season: 1,
seasonType: 1,
week: 1,
timestamp: 1,
} as ProjectionType<any>,
)
.lean()
.exec(); |
@vkarpov15 Nice! Thank you! |
Re: #15327
Summary
@pshaddel please take a look as well.
Simplified alternative to #15327 that seems to compile with Typegoose. The one major tradeoff is that it only considers 1 level deep nested paths, as opposed to 3 in #15327. However, that should be sufficient for most use cases. There's also the added benefit that this PR reuses the existing
With1LevelDeepNestedPaths
type, with some fixes to handle arrays, which should reduce risk.Examples