Skip to content

Commit

Permalink
Merge pull request #14819 from Automattic/vkarpov15/gh-12883
Browse files Browse the repository at this point in the history
types: make toObject() and toJSON() not generic by default to avoid type widening
  • Loading branch information
vkarpov15 authored Aug 20, 2024
2 parents ca0191c + 7bc60e9 commit 8180a73
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
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

0 comments on commit 8180a73

Please sign in to comment.