11import { Document , ObjectId } from 'bson' ;
22import { expectAssignable , expectError , expectNotType , expectType } from 'tsd' ;
33
4+ import type { Collection } from '../../src' ;
45import 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+
1418expectType < InferIdType < Document > > ( new ObjectId ( ) ) ;
1519expectType < InferIdType < { _id : number } > > ( 1 + 1 ) ;
1620expectType < InferIdType < { a : number } | { b : string } > > ( new ObjectId ( ) ) ;
@@ -22,13 +26,20 @@ expectError<InferIdType<{ _id: Record<string, any> }>>({});
2226expectAssignable < InferIdType < { _id : number } | { b : string } > > ( new ObjectId ( ) ) ;
2327expectAssignable < InferIdType < { _id : number } | { b : string } > > ( 1 + 1 ) ;
2428
25- // WithId
29+ /** ----------------------------------------------------------------------
30+ * WithId
31+ */
32+
2633expectAssignable < WithId < Document > > ( { _id : new ObjectId ( ) } ) ;
2734expectAssignable < WithId < { a : number } > > ( { _id : new ObjectId ( ) , a : 3 } ) ;
2835expectAssignable < WithId < { _id : ObjectId } > > ( { _id : new ObjectId ( ) } ) ;
2936expectAssignable < WithId < { _id : number } > > ( { _id : 5 } ) ;
3037expectNotType < WithId < Document > > ( { _id : 3 } ) ;
3138
39+ /** ----------------------------------------------------------------------
40+ * OptionalId
41+ */
42+
3243// Changing _id to a type other than ObjectId makes it required:
3344expectNotType < OptionalId < { _id : number ; a : number } > > ( { a : 3 } ) ;
3445expectNotType < OptionalId < { _id : number ; a : number } | { _id : ObjectId ; b : number } > > ( { a : 3 } ) ;
@@ -44,20 +55,105 @@ class MyId {}
4455expectNotType < OptionalId < { _id : MyId ; a : number } > > ( { a : 3 } ) ;
4556expectNotType < 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+
47134declare function functionReturningOptionalId ( ) : OptionalId < {
48135 _id ?: ObjectId | undefined ;
49136 a : number ;
50137} > ;
51- // OptionalUnlessRequiredId
52138expectType < OptionalUnlessRequiredId < { _id : ObjectId ; a : number } > > ( { a : 3 , _id : new ObjectId ( ) } ) ;
53139expectType < 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+
56147expectType < WithoutId < { _id : number ; a : number } > > ( { a : 2 } ) ;
57148expectNotType < WithoutId < { _id : number ; a : number } > > ( { _id : 3 , a : 2 } ) ;
58149expectNotType < 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