Skip to content

Commit

Permalink
Merge pull request #14612 from Automattic/vkarpov15/gh-14601
Browse files Browse the repository at this point in the history
types: pass DocType down to subdocuments so `HydratedSingleSubdocument` and `HydratedArraySubdocument` `toObject()` returns correct type
  • Loading branch information
vkarpov15 authored May 30, 2024
2 parents 90f9e55 + 2290c81 commit ca09111
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
40 changes: 39 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,32 @@ 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 f2obj = item.f2.toObject();
expectAssignable<{ _id: Types.ObjectId, field1: string }>(f2obj);
}
4 changes: 2 additions & 2 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,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 @@ -96,7 +96,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 ca09111

Please sign in to comment.