1
1
import { Document , ObjectId } from 'bson' ;
2
2
import { expectAssignable , expectError , expectNotType , expectType } from 'tsd' ;
3
3
4
+ import type { Collection } from '../../src' ;
4
5
import type {
5
6
EnhancedOmit ,
6
7
InferIdType ,
@@ -10,7 +11,10 @@ import type {
10
11
WithoutId
11
12
} from '../../src/mongo_types' ;
12
13
13
- // InferIdType
14
+ /** ----------------------------------------------------------------------
15
+ * InferIdType
16
+ */
17
+
14
18
expectType < InferIdType < Document > > ( new ObjectId ( ) ) ;
15
19
expectType < InferIdType < { _id : number } > > ( 1 + 1 ) ;
16
20
expectType < InferIdType < { a : number } | { b : string } > > ( new ObjectId ( ) ) ;
@@ -22,13 +26,20 @@ expectError<InferIdType<{ _id: Record<string, any> }>>({});
22
26
expectAssignable < InferIdType < { _id : number } | { b : string } > > ( new ObjectId ( ) ) ;
23
27
expectAssignable < InferIdType < { _id : number } | { b : string } > > ( 1 + 1 ) ;
24
28
25
- // WithId
29
+ /** ----------------------------------------------------------------------
30
+ * WithId
31
+ */
32
+
26
33
expectAssignable < WithId < Document > > ( { _id : new ObjectId ( ) } ) ;
27
34
expectAssignable < WithId < { a : number } > > ( { _id : new ObjectId ( ) , a : 3 } ) ;
28
35
expectAssignable < WithId < { _id : ObjectId } > > ( { _id : new ObjectId ( ) } ) ;
29
36
expectAssignable < WithId < { _id : number } > > ( { _id : 5 } ) ;
30
37
expectNotType < WithId < Document > > ( { _id : 3 } ) ;
31
38
39
+ /** ----------------------------------------------------------------------
40
+ * OptionalId
41
+ */
42
+
32
43
// Changing _id to a type other than ObjectId makes it required:
33
44
expectNotType < OptionalId < { _id : number ; a : number } > > ( { a : 3 } ) ;
34
45
expectNotType < OptionalId < { _id : number ; a : number } | { _id : ObjectId ; b : number } > > ( { a : 3 } ) ;
@@ -44,20 +55,105 @@ class MyId {}
44
55
expectNotType < OptionalId < { _id : MyId ; a : number } > > ( { a : 3 } ) ;
45
56
expectNotType < OptionalId < { _id : MyId ; a : number } > > ( { _id : new ObjectId ( ) , a : 3 } ) ;
46
57
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
+
47
134
declare function functionReturningOptionalId ( ) : OptionalId < {
48
135
_id ?: ObjectId | undefined ;
49
136
a : number ;
50
137
} > ;
51
- // OptionalUnlessRequiredId
52
138
expectType < OptionalUnlessRequiredId < { _id : ObjectId ; a : number } > > ( { a : 3 , _id : new ObjectId ( ) } ) ;
53
139
expectType < OptionalUnlessRequiredId < { _id ?: ObjectId ; a : number } > > ( functionReturningOptionalId ( ) ) ;
54
140
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
+
56
147
expectType < WithoutId < { _id : number ; a : number } > > ( { a : 2 } ) ;
57
148
expectNotType < WithoutId < { _id : number ; a : number } > > ( { _id : 3 , a : 2 } ) ;
58
149
expectNotType < WithoutId < { a : number } > > ( { _id : 3 , a : 2 } ) ;
59
150
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
+
61
157
// NODE-3287
62
158
// expectNotAssignable<EnhancedOmit<{ a: 'one' } | { b: 'two' }, 'a'>>({
63
159
// a: 'one' as const
0 commit comments