Skip to content

Commit d56414f

Browse files
authored
test(NODE-4498): ts tests for OptionalId wrapping schemas with _id (#3486)
1 parent e3ead58 commit d56414f

File tree

1 file changed

+101
-5
lines changed

1 file changed

+101
-5
lines changed

test/types/schema_helpers.test-d.ts

Lines changed: 101 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Document, ObjectId } from 'bson';
22
import { expectAssignable, expectError, expectNotType, expectType } from 'tsd';
33

4+
import type { Collection } from '../../src';
45
import type {
56
EnhancedOmit,
67
InferIdType,
@@ -10,7 +11,10 @@ import type {
1011
WithoutId
1112
} from '../../src/mongo_types';
1213

13-
// InferIdType
14+
/** ----------------------------------------------------------------------
15+
* InferIdType
16+
*/
17+
1418
expectType<InferIdType<Document>>(new ObjectId());
1519
expectType<InferIdType<{ _id: number }>>(1 + 1);
1620
expectType<InferIdType<{ a: number } | { b: string }>>(new ObjectId());
@@ -22,13 +26,20 @@ expectError<InferIdType<{ _id: Record<string, any> }>>({});
2226
expectAssignable<InferIdType<{ _id: number } | { b: string }>>(new ObjectId());
2327
expectAssignable<InferIdType<{ _id: number } | { b: string }>>(1 + 1);
2428

25-
// WithId
29+
/** ----------------------------------------------------------------------
30+
* WithId
31+
*/
32+
2633
expectAssignable<WithId<Document>>({ _id: new ObjectId() });
2734
expectAssignable<WithId<{ a: number }>>({ _id: new ObjectId(), a: 3 });
2835
expectAssignable<WithId<{ _id: ObjectId }>>({ _id: new ObjectId() });
2936
expectAssignable<WithId<{ _id: number }>>({ _id: 5 });
3037
expectNotType<WithId<Document>>({ _id: 3 });
3138

39+
/** ----------------------------------------------------------------------
40+
* OptionalId
41+
*/
42+
3243
// Changing _id to a type other than ObjectId makes it required:
3344
expectNotType<OptionalId<{ _id: number; a: number }>>({ a: 3 });
3445
expectNotType<OptionalId<{ _id: number; a: number } | { _id: ObjectId; b: number }>>({ a: 3 });
@@ -44,20 +55,105 @@ class MyId {}
4455
expectNotType<OptionalId<{ _id: MyId; a: number }>>({ a: 3 });
4556
expectNotType<OptionalId<{ _id: MyId; a: number }>>({ _id: new ObjectId(), a: 3 });
4657

58+
// OptionalId assignability when wrapping a schema with _id: ObjectId
59+
type SchemaWithIdType = {
60+
_id: ObjectId;
61+
a: number;
62+
};
63+
interface SchemaWithIdInterface {
64+
_id: ObjectId;
65+
a: number;
66+
}
67+
declare const typeTestCollection: Collection<OptionalId<SchemaWithIdType>>;
68+
declare const interfaceTestCollection: Collection<OptionalId<SchemaWithIdInterface>>;
69+
70+
expectAssignable<SchemaWithIdType | null>(await typeTestCollection.findOne());
71+
expectAssignable<SchemaWithIdInterface | null>(await interfaceTestCollection.findOne());
72+
expectAssignable<SchemaWithIdType>((await typeTestCollection.find().toArray())[0]);
73+
expectAssignable<SchemaWithIdInterface>((await interfaceTestCollection.find().toArray())[0]);
74+
expectAssignable<SchemaWithIdType | null>(
75+
(await typeTestCollection.findOneAndDelete({ a: 1 })).value
76+
);
77+
expectAssignable<SchemaWithIdInterface | null>(
78+
(await interfaceTestCollection.findOneAndDelete({ a: 1 })).value
79+
);
80+
expectAssignable<SchemaWithIdType | null>(
81+
(await typeTestCollection.findOneAndReplace({ a: 1 }, { a: 5 })).value
82+
);
83+
expectAssignable<SchemaWithIdInterface | null>(
84+
(await interfaceTestCollection.findOneAndReplace({ a: 1 }, { a: 5 })).value
85+
);
86+
expectAssignable<SchemaWithIdType | null>(
87+
(await typeTestCollection.findOneAndUpdate({ a: 1 }, { a: 5 })).value
88+
);
89+
expectAssignable<SchemaWithIdInterface | null>(
90+
(await interfaceTestCollection.findOneAndUpdate({ a: 1 }, { a: 5 })).value
91+
);
92+
93+
// OptionalId assignability when wrapping a schema with _id: number
94+
type SchemaWithIdNumberType = {
95+
_id: number;
96+
a: number;
97+
};
98+
interface SchemaWithIdNumberInterface {
99+
_id: number;
100+
a: number;
101+
}
102+
declare const typeNumberTestCollection: Collection<OptionalId<SchemaWithIdNumberType>>;
103+
declare const interfaceNumberTestCollection: Collection<OptionalId<SchemaWithIdNumberInterface>>;
104+
105+
expectAssignable<SchemaWithIdNumberType | null>(await typeNumberTestCollection.findOne());
106+
expectAssignable<SchemaWithIdNumberInterface | null>(await interfaceNumberTestCollection.findOne());
107+
expectAssignable<SchemaWithIdNumberType>((await typeNumberTestCollection.find().toArray())[0]);
108+
expectAssignable<SchemaWithIdNumberInterface>(
109+
(await interfaceNumberTestCollection.find().toArray())[0]
110+
);
111+
expectAssignable<SchemaWithIdNumberType | null>(
112+
(await typeNumberTestCollection.findOneAndDelete({ a: 1 })).value
113+
);
114+
expectAssignable<SchemaWithIdNumberInterface | null>(
115+
(await interfaceNumberTestCollection.findOneAndDelete({ a: 1 })).value
116+
);
117+
expectAssignable<SchemaWithIdNumberType | null>(
118+
(await typeNumberTestCollection.findOneAndReplace({ a: 1 }, { a: 5 })).value
119+
);
120+
expectAssignable<SchemaWithIdNumberInterface | null>(
121+
(await interfaceNumberTestCollection.findOneAndReplace({ a: 1 }, { a: 5 })).value
122+
);
123+
expectAssignable<SchemaWithIdNumberType | null>(
124+
(await typeNumberTestCollection.findOneAndUpdate({ a: 1 }, { a: 5 })).value
125+
);
126+
expectAssignable<SchemaWithIdNumberInterface | null>(
127+
(await interfaceNumberTestCollection.findOneAndUpdate({ a: 1 }, { a: 5 })).value
128+
);
129+
130+
/** ----------------------------------------------------------------------
131+
* OptionalUnlessRequiredId
132+
*/
133+
47134
declare function functionReturningOptionalId(): OptionalId<{
48135
_id?: ObjectId | undefined;
49136
a: number;
50137
}>;
51-
// OptionalUnlessRequiredId
52138
expectType<OptionalUnlessRequiredId<{ _id: ObjectId; a: number }>>({ a: 3, _id: new ObjectId() });
53139
expectType<OptionalUnlessRequiredId<{ _id?: ObjectId; a: number }>>(functionReturningOptionalId());
54140

55-
// WithoutId removes _id whether defined in the schema or not
141+
/** ----------------------------------------------------------------------
142+
* WithoutId
143+
*
144+
* WithoutId removes _id whether defined in the schema or not
145+
*/
146+
56147
expectType<WithoutId<{ _id: number; a: number }>>({ a: 2 });
57148
expectNotType<WithoutId<{ _id: number; a: number }>>({ _id: 3, a: 2 });
58149
expectNotType<WithoutId<{ a: number }>>({ _id: 3, a: 2 });
59150

60-
// EnhancedOmit fixes a problem with Typescript's built in Omit that breaks discriminated unions
151+
/** ----------------------------------------------------------------------
152+
* EnhancedOmit
153+
*
154+
* EnhancedOmit fixes a problem with Typescript's built in Omit that breaks discriminated unions
155+
*/
156+
61157
// NODE-3287
62158
// expectNotAssignable<EnhancedOmit<{ a: 'one' } | { b: 'two' }, 'a'>>({
63159
// a: 'one' as const

0 commit comments

Comments
 (0)