Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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