Skip to content

Commit

Permalink
types: improve function parameter types for ToObjectOptions transfo…
Browse files Browse the repository at this point in the history
…rm option

Fix #13421
  • Loading branch information
vkarpov15 committed May 26, 2023
1 parent a29b45e commit ef62c1d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
8 changes: 6 additions & 2 deletions types/index.d.ts
Expand Up @@ -161,7 +161,7 @@ declare module 'mongoose' {
[k: string]: string;
}

export interface ToObjectOptions {
export interface ToObjectOptions<THydratedDocumentType = HydratedDocument<unknown>> {
/** apply all getters (path and virtual getters) */
getters?: boolean;
/** apply virtual getters (can override getters option) */
Expand All @@ -171,7 +171,11 @@ declare module 'mongoose' {
/** remove empty objects (defaults to true) */
minimize?: boolean;
/** if set, mongoose will call this function to allow you to transform the returned object */
transform?: boolean | ((doc: any, ret: any, options: any) => any);
transform?: boolean | ((
doc: THydratedDocumentType,
ret: Record<string, any>,
options: ToObjectOptions<THydratedDocumentType>
) => any);
/** if true, replace any conventionally populated paths with the original id in the output. Has no affect on virtual populated paths. */
depopulate?: boolean;
/** if false, exclude the version key (`__v` by default) from the output */
Expand Down
4 changes: 2 additions & 2 deletions types/schemaoptions.d.ts
Expand Up @@ -133,14 +133,14 @@ declare module 'mongoose' {
*/
strictQuery?: boolean | 'throw';
/** Exactly the same as the toObject option but only applies when the document's toJSON method is called. */
toJSON?: ToObjectOptions;
toJSON?: ToObjectOptions<THydratedDocumentType>;
/**
* Documents have a toObject method which converts the mongoose document into a plain JavaScript object.
* This method accepts a few options. Instead of applying these options on a per-document basis, we may
* declare the options at the schema level and have them applied to all of the schema's documents by
* default.
*/
toObject?: ToObjectOptions;
toObject?: ToObjectOptions<THydratedDocumentType>;
/**
* By default, if you have an object with key 'type' in your schema, mongoose will interpret it as a
* type declaration. However, for applications like geoJSON, the 'type' property is important. If you want to
Expand Down
4 changes: 2 additions & 2 deletions types/types.d.ts
Expand Up @@ -42,8 +42,8 @@ declare module 'mongoose' {
shift(): T;

/** Returns a native js Array. */
toObject(options?: ToObjectOptions): any;
toObject<T>(options?: ToObjectOptions): T;
toObject(options?: ToObjectOptions<T>): any;
toObject<T>(options?: ToObjectOptions<T>): T;

/** Wraps [`Array#unshift`](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/unshift) with proper change tracking. */
unshift(...args: any[]): number;
Expand Down

0 comments on commit ef62c1d

Please sign in to comment.