Skip to content

Commit

Permalink
Refactor UnpackedIntersection type & some tests related to populate FN
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammad0-0ahmad committed Mar 27, 2022
1 parent 4315ff5 commit f286273
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
9 changes: 7 additions & 2 deletions test/types/populate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,14 @@ function gh11503() {
const User = model<User>('friends', userSchema);

User.findOne({}).populate('friends').then(user => {
expectType<Types.ObjectId | undefined>(user?.friends[0]);
if (!user) return;
expectType<Types.ObjectId>(user?.friends[0]);
expectError(user?.friends[0].blocked);
expectError(user?.friends.map(friend => friend.blocked));
});

User.findOne({}).populate<{friends: Friend[]}>('friends').then(user => {
User.findOne({}).populate<{ friends: Friend[] }>('friends').then(user => {
if (!user) return;
expectAssignable<Friend>(user?.friends[0]);
expectType<boolean>(user?.friends[0].blocked);
const firstFriendBlockedValue = user?.friends.map(friend => friend)[0];
Expand Down Expand Up @@ -220,6 +222,9 @@ async function _11532() {
const populateResult = await populateQuery;
const leanResult = await populateQuery.lean();

if (!populateResult) return;
expectType<string>(populateResult.child.name);

if (!leanResult) return;
expectType<string>(leanResult.child.name);
}
2 changes: 1 addition & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1469,7 +1469,7 @@ declare module 'mongoose' {

type QueryWithHelpers<ResultType, DocType, THelpers = {}, RawDocType = DocType> = Query<ResultType, DocType, THelpers, RawDocType> & THelpers;

type UnpackedIntersection<T, U> = T extends (infer A)[]
type UnpackedIntersection<T, U> = T extends null ? null : T extends (infer A)[]
? (Omit<A, keyof U> & U)[]
: keyof U extends never
? T
Expand Down

0 comments on commit f286273

Please sign in to comment.