Skip to content

Commit

Permalink
types: pass DocType down to subdocuments so `HydratedSingleSubdocumen…
Browse files Browse the repository at this point in the history
…t` and `HydratedArraySubdocument` `toObject()` returns correct type

Fix #14601
  • Loading branch information
vkarpov15 committed Jun 5, 2024
1 parent 7b6be47 commit 40ec813
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
43 changes: 42 additions & 1 deletion test/types/subdocuments.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import { Schema, model, Model, Document, Types } from 'mongoose';
import {
Schema,
model,
Model,
Document,
Types,
HydratedArraySubdocument,
HydratedSingleSubdocument
} from 'mongoose';
import { expectAssignable } from 'tsd';

const childSchema: Schema = new Schema({ name: String });

Expand Down Expand Up @@ -108,3 +117,35 @@ function gh13040(): void {
product.ownerDocument();
});
}

function gh14601() {
interface ISub {
field1: string;
}
interface IMain {
f1: string;
f2: HydratedSingleSubdocument<ISub>;
f3: HydratedArraySubdocument<ISub>[];
}

const subSchema = new Schema({ field1: String }, { _id: false });

const mainSchema = new Schema({
f1: String,
f2: { type: subSchema },
f3: { type: [subSchema] }
});
const MainModel = model<IMain>('Main', mainSchema);

const item = new MainModel({
f1: 'test',
f2: { field1: 'test' },
f3: [{ field1: 'test' }]
});

const obj = item.toObject();

const obj2 = item.f2.toObject();

expectAssignable<{ _id: Types.ObjectId, field1: string }>(obj2);
}
4 changes: 2 additions & 2 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ declare module 'mongoose' {
>
>
>;
export type HydratedSingleSubdocument<DocType, TOverrides = {}> = Types.Subdocument<unknown> & Require_id<DocType> & TOverrides;
export type HydratedArraySubdocument<DocType, TOverrides = {}> = Types.ArraySubdocument<unknown> & Require_id<DocType> & TOverrides;
export type HydratedSingleSubdocument<DocType, TOverrides = {}> = Types.Subdocument<unknown, Record<string, never>, DocType> & Require_id<DocType> & TOverrides;
export type HydratedArraySubdocument<DocType, TOverrides = {}> = Types.ArraySubdocument<unknown, Record<string, never>, DocType> & Require_id<DocType> & TOverrides;

export type HydratedDocumentFromSchema<TSchema extends Schema> = HydratedDocument<
InferSchemaType<TSchema>,
Expand Down
2 changes: 1 addition & 1 deletion types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ declare module 'mongoose' {
$parent(): Document;
}

class ArraySubdocument<IdType = any> extends Subdocument<IdType> {
class ArraySubdocument<IdType = any, TQueryHelpers = unknown, DocType = unknown> extends Subdocument<IdType, TQueryHelpers, DocType> {
/** Returns this sub-documents parent array. */
parentArray(): Types.DocumentArray<unknown>;
}
Expand Down

0 comments on commit 40ec813

Please sign in to comment.