Skip to content

Commit ebbebd7

Browse files
Refact UnpackedIntersection type
1 parent 3c57076 commit ebbebd7

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

test/types/populate.test.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,13 @@ function gh11496() {
176176

177177
Users.findOne({}).populate<{friends: Friend[]}>('friends').then(user => {
178178
expectAssignable<Friend | undefined>(user?.friends[0]);
179-
user?.friends[0].blocked;
180-
user?.friends.map(friend => friend.blocked);
179+
expectType<boolean | undefined>(user?.friends[0].blocked);
180+
expectType<Types.ObjectId | undefined>(user?.friends[0]._id);
181+
182+
const firstFriendBlockedValue = user?.friends.map(friend => friend)[0];
183+
184+
// FIXME: These two test doesn't work, although it shows correct suggestions.
185+
expectType<boolean | undefined>(firstFriendBlockedValue?.blocked);
186+
expectType<Types.ObjectId>(firstFriendBlockedValue?._id);
181187
});
182188
}

types/index.d.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1812,8 +1812,13 @@ declare module 'mongoose' {
18121812
type ReturnsNewDoc = { new: true } | { returnOriginal: false } | {returnDocument: 'after'};
18131813

18141814
type QueryWithHelpers<ResultType, DocType, THelpers = {}, RawDocType = DocType> = Query<ResultType, DocType, THelpers, RawDocType> & THelpers;
1815+
// type UnpackedIntersection<T, U> = T extends (infer V)[] ? (V & U)[] : T & U;
18151816

1816-
type UnpackedIntersection<T, U> = T extends (infer A)[] ? (A & U)[] : keyof U extends never ? T & U: { [P in keyof U]: U[P] extends (infer B)[] ? (B & T)[] : U[P] & T };
1817+
type UnpackedIntersection<T, U> = T extends (infer A)[]
1818+
? (A & U)[]
1819+
: keyof U extends never
1820+
? T
1821+
: { [P in keyof U]: U[P] extends (infer B)[] ? (T & B)[] : T & U[P] };
18171822

18181823
type UnpackedIntersectionWithNull<T, U> = T extends null ? UnpackedIntersection<T, U> | null : UnpackedIntersection<T, U>;
18191824

0 commit comments

Comments
 (0)