Skip to content

Commit 6c36263

Browse files
committedAug 21, 2021
fix(index.d.ts): allow strings for ObjectIds in nested properties
Fix #10573
1 parent e90aab1 commit 6c36263

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed
 

‎index.d.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -1270,10 +1270,10 @@ declare module 'mongoose' {
12701270
/**
12711271
* Create a new schema
12721272
*/
1273-
constructor(definition?: SchemaDefinition<DocumentDefinition<SchemaDefinitionType>>, options?: SchemaOptions);
1273+
constructor(definition?: SchemaDefinition<LeanDocument<SchemaDefinitionType>>, options?: SchemaOptions);
12741274

12751275
/** Adds key path / schema type pairs to this schema. */
1276-
add(obj: SchemaDefinition<DocumentDefinition<SchemaDefinitionType>> | Schema, prefix?: string): this;
1276+
add(obj: SchemaDefinition<LeanDocument<SchemaDefinitionType>> | Schema, prefix?: string): this;
12771277

12781278
/**
12791279
* Array of child schemas (from document arrays and single nested subdocs)
@@ -2587,8 +2587,12 @@ declare module 'mongoose' {
25872587
export type UpdateQuery<T> = (_UpdateQuery<DocumentDefinition<T>> & mongodb.MatchKeysAndValues<DocumentDefinition<T>>);
25882588

25892589
type _AllowStringsForIds<T> = {
2590-
[K in keyof T]: [Extract<T[K], mongodb.ObjectId>] extends [never] ? T[K] : T[K] | string;
2591-
};
2590+
[K in keyof T]: [Extract<T[K], mongodb.ObjectId>] extends [never]
2591+
? T[K] extends TreatAsPrimitives
2592+
? T[K]
2593+
: _AllowStringsForIds<T[K]>
2594+
: T[K] | string;
2595+
};
25922596
export type DocumentDefinition<T> = _AllowStringsForIds<LeanDocument<T>>;
25932597

25942598
type actualPrimitives = string | boolean | number | bigint | symbol | null | undefined;

‎test/typescript/queries.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ interface QueryHelpers {
88
const schema: Schema<ITest, Model<ITest, QueryHelpers>> = new Schema({
99
name: { type: 'String' },
1010
tags: [String],
11-
docs: [{ nested: { id: Number, tags: [String] } }],
11+
docs: [{ _id: 'ObjectId', id: Number, tags: [String] }],
1212
endDate: Date
1313
});
1414

@@ -22,6 +22,7 @@ schema.query.byName = function(name: string): QueryWithHelpers<any, ITest> {
2222
};
2323

2424
interface ISubdoc extends Document {
25+
myId?: Types.ObjectId;
2526
id?: number;
2627
tags?: string[];
2728
}
@@ -89,6 +90,8 @@ Test.findOneAndUpdate({ name: 'test' }, [{ $set: { endDate: true } }]);
8990

9091
Test.findByIdAndUpdate({ name: 'test' }, { name: 'test2' }, (err, doc) => console.log(doc));
9192

93+
Test.findOneAndUpdate({ name: 'test' }, { 'docs.0.myId': '0'.repeat(24) });
94+
9295
const query: Query<ITest | null, ITest> = Test.findOne();
9396
query instanceof Query;
9497

0 commit comments

Comments
 (0)
Please sign in to comment.