Skip to content

Commit

Permalink
types: improve return value for Types.Subdocument.cast()
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Jul 23, 2024
1 parent 9da1804 commit 351b289
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
7 changes: 6 additions & 1 deletion test/types/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1565,8 +1565,13 @@ function gh14748() {
singleNested: nestedSchema
});

// Cast to single nested subdoc
const subdoc = schema.path('singleNested')
.cast<HydratedArraySubdocument<{ name: string }>>({ name: 'bar' });
expectAssignable<{ name: string }>(subdoc);

const subdoc2 = schema.path('singleNested').cast({ name: 'bar' });
expectAssignable<{ name: string }>(subdoc2);

const subdoc3 = schema.path<Schema.Types.Subdocument<{ name: string }>>('singleNested').cast({ name: 'bar' });
expectAssignable<{ name: string }>(subdoc3);
}
4 changes: 3 additions & 1 deletion types/schematypes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ declare module 'mongoose' {
defaultOptions: Record<string, any>;
}

class Subdocument extends SchemaType implements AcceptsDiscriminator {
class Subdocument<DocType = unknown> extends SchemaType implements AcceptsDiscriminator {
/** This schema type's name, to defend against minifiers that mangle function names. */
static schemaName: string;

Expand All @@ -456,6 +456,8 @@ declare module 'mongoose' {

discriminator<T, U>(name: string | number, schema: Schema<T, U>, value?: string): U;
discriminator<D>(name: string | number, schema: Schema, value?: string): Model<D>;

cast(val: any, doc?: Document<any>, init?: boolean, prev?: any, options?: any): HydratedSingleSubdocument<DocType>;
}

class String extends SchemaType {
Expand Down

0 comments on commit 351b289

Please sign in to comment.