Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

types: improve function parameter types for ToObjectOptions transform option #13446

Merged
merged 1 commit into from
May 27, 2023
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
8 changes: 6 additions & 2 deletions types/index.d.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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