From 351b28957871cb371da10204b0986efa2f49d915 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Tue, 23 Jul 2024 12:37:55 -0400 Subject: [PATCH] types: improve return value for Types.Subdocument.cast() --- test/types/schema.test.ts | 7 ++++++- types/schematypes.d.ts | 4 +++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/test/types/schema.test.ts b/test/types/schema.test.ts index b672d14e27e..a9679c082b8 100644 --- a/test/types/schema.test.ts +++ b/test/types/schema.test.ts @@ -1565,8 +1565,13 @@ function gh14748() { singleNested: nestedSchema }); - // Cast to single nested subdoc const subdoc = schema.path('singleNested') .cast>({ name: 'bar' }); expectAssignable<{ name: string }>(subdoc); + + const subdoc2 = schema.path('singleNested').cast({ name: 'bar' }); + expectAssignable<{ name: string }>(subdoc2); + + const subdoc3 = schema.path>('singleNested').cast({ name: 'bar' }); + expectAssignable<{ name: string }>(subdoc3); } diff --git a/types/schematypes.d.ts b/types/schematypes.d.ts index 26eab700a81..e8a0ecffdf0 100644 --- a/types/schematypes.d.ts +++ b/types/schematypes.d.ts @@ -444,7 +444,7 @@ declare module 'mongoose' { defaultOptions: Record; } - class Subdocument extends SchemaType implements AcceptsDiscriminator { + class Subdocument extends SchemaType implements AcceptsDiscriminator { /** This schema type's name, to defend against minifiers that mangle function names. */ static schemaName: string; @@ -456,6 +456,8 @@ declare module 'mongoose' { discriminator(name: string | number, schema: Schema, value?: string): U; discriminator(name: string | number, schema: Schema, value?: string): Model; + + cast(val: any, doc?: Document, init?: boolean, prev?: any, options?: any): HydratedSingleSubdocument; } class String extends SchemaType {