Skip to content
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
29 changes: 29 additions & 0 deletions test/types/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1812,3 +1812,32 @@ function defaultReturnsUndefined() {
}
});
}

function gh15479() {
const TestSchema = new Schema({
name: String,
testField: {
type: String,
required: true,
default: 'blah'
}
});

function transform(doc: unknown, ret: InferSchemaType<typeof TestSchema>) {
const { testField, ...val } = ret;
return val;
}

TestSchema.set('toJSON', { transform });

const TestModel = model('Test', TestSchema);

const doc = new TestModel();

getTestField(doc.toJSON<ReturnType<typeof transform> & { testField: string }>());
expectError(getTestField(doc.toJSON<ReturnType<typeof transform>>()));

function getTestField(obj: { testField: string }) {
return obj.testField;
}
}
6 changes: 3 additions & 3 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ declare module 'mongoose' {
[k: string]: string;
}

export interface ToObjectOptions<THydratedDocumentType = HydratedDocument<unknown>> {
export interface ToObjectOptions<RawDocType = unknown, THydratedDocumentType = HydratedDocument<RawDocType>> {
/** if `options.virtuals = true`, you can set `options.aliases = false` to skip applying aliases. This option is a no-op if `options.virtuals = false`. */
aliases?: boolean;
/** if true, replace any conventionally populated paths with the original id in the output. Has no affect on virtual populated paths. */
Expand All @@ -224,7 +224,7 @@ declare module 'mongoose' {
/** if set, mongoose will call this function to allow you to transform the returned object */
transform?: boolean | ((
doc: THydratedDocumentType,
ret: Record<string, any>,
ret: RawDocType,
options: ToObjectOptions<THydratedDocumentType>
) => any);
/** If true, omits fields that are excluded in this document's projection. Unless you specified a projection, this will omit any field that has `select: false` in the schema. */
Expand Down Expand Up @@ -505,7 +505,7 @@ declare module 'mongoose' {
requiredPaths(invalidate?: boolean): string[];

/** Sets a schema option. */
set<K extends keyof SchemaOptions>(key: K, value: SchemaOptions[K], _tags?: any): this;
set<K extends keyof SchemaOptions>(key: K, value: SchemaOptions<DocType>[K], _tags?: any): this;

/** Adds static "class" methods to Models compiled from this schema. */
static<K extends keyof TStaticMethods>(name: K, fn: TStaticMethods[K]): this;
Expand Down
4 changes: 2 additions & 2 deletions types/schemaoptions.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,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<THydratedDocumentType>;
toJSON?: ToObjectOptions<DocType, 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<THydratedDocumentType>;
toObject?: ToObjectOptions<DocType, 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