Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 6 additions & 3 deletions test/types/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
model,
ValidateOpts
} from 'mongoose';
import { IsPathRequired } from '../../types/inferschematype';
import { expectType, expectError, expectAssignable } from 'tsd';
import { ObtainDocumentPathType, ResolvePathType } from '../../types/inferschematype';

Expand Down Expand Up @@ -1502,16 +1503,18 @@ function gh13772() {
const schemaDefinition = {
name: String,
docArr: [{ name: String }]
};
} as const;
const schema = new Schema(schemaDefinition);

const TestModel = model('User', schema);
type RawDocType = InferRawDocType<typeof schemaDefinition>;
expectAssignable<
{ name?: string | null, docArr?: Array<{ name?: string | null }> }
{ name?: string | null, docArr?: Array<{ name?: string | null }> | null }
>({} as RawDocType);

const TestModel = model('User', schema);
const doc = new TestModel();
expectAssignable<RawDocType>(doc.toObject());
expectAssignable<RawDocType>(doc.toJSON());
}

function gh14696() {
Expand Down
5 changes: 4 additions & 1 deletion types/document.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,11 +259,14 @@ declare module 'mongoose' {
set(value: string | Record<string, any>): this;

/** The return value of this method is used in calls to JSON.stringify(doc). */
toJSON(options?: ToObjectOptions & { flattenMaps?: true }): FlattenMaps<Require_id<DocType>>;
toJSON(options: ToObjectOptions & { flattenMaps: false }): Require_id<DocType>;
toJSON<T = Require_id<DocType>>(options?: ToObjectOptions & { flattenMaps?: true }): FlattenMaps<T>;
toJSON<T = Require_id<DocType>>(options: ToObjectOptions & { flattenMaps: false }): T;

/** Converts this document into a plain-old JavaScript object ([POJO](https://masteringjs.io/tutorials/fundamentals/pojo)). */
toObject<T = Require_id<DocType>>(options?: ToObjectOptions): Require_id<T>;
toObject(options?: ToObjectOptions): Require_id<DocType>;
toObject<T>(options?: ToObjectOptions): Require_id<T>;

/** Clears the modified state on the specified path. */
unmarkModified<T extends keyof DocType>(path: T): void;
Expand Down
5 changes: 4 additions & 1 deletion types/inferrawdoctype.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
IsPathRequired,
IsSchemaTypeFromBuiltinClass,
RequiredPaths,
OptionalPaths,
Expand All @@ -14,7 +15,9 @@ declare module 'mongoose' {
[
K in keyof (RequiredPaths<DocDefinition, TSchemaOptions['typeKey']> &
OptionalPaths<DocDefinition, TSchemaOptions['typeKey']>)
]: ObtainRawDocumentPathType<DocDefinition[K], TSchemaOptions['typeKey']>;
]: IsPathRequired<DocDefinition[K], TSchemaOptions['typeKey']> extends true
? ObtainRawDocumentPathType<DocDefinition[K], TSchemaOptions['typeKey']>
: ObtainRawDocumentPathType<DocDefinition[K], TSchemaOptions['typeKey']> | null;
}, TSchemaOptions>;

/**
Expand Down