From 505d6a08afe221707aa61f4e3a0a589f9c802ae9 Mon Sep 17 00:00:00 2001 From: Phillip Huang Date: Fri, 28 Nov 2025 19:29:07 -0600 Subject: [PATCH 1/2] Export InferPojoType to replicate Mongoose 8 InferRawDocType behavior --- test/types/inferrawdoctype.test.ts | 26 ++++++++++++++++++++++++-- types/inferrawdoctype.d.ts | 8 +++++++- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/test/types/inferrawdoctype.test.ts b/test/types/inferrawdoctype.test.ts index c67ae090fa..743bb85be7 100644 --- a/test/types/inferrawdoctype.test.ts +++ b/test/types/inferrawdoctype.test.ts @@ -1,6 +1,28 @@ -import { InferRawDocType, type InferSchemaType, type ResolveTimestamps, type Schema, type Types } from 'mongoose'; -import { expectType, expectError } from 'tsd'; +import { InferRawDocType, type InferPojoType, type ResolveTimestamps, type Schema, type Types } from 'mongoose'; +import { expectType } from 'tsd'; +function inferPojoType() { + const schemaDefinition = { + email: { + type: String, + trim: true, + required: true, + unique: true, + lowercase: true + }, + password: { + type: String, + required: true + }, + dateOfBirth: { + type: Date, + required: true + } + }; + + type UserType = InferPojoType< typeof schemaDefinition>; + expectType<{ email: string, password: string, dateOfBirth: Date }>({} as UserType); +} function gh14839() { const schemaDefinition = { email: { diff --git a/types/inferrawdoctype.d.ts b/types/inferrawdoctype.d.ts index 9e54ef3ea0..6c49ccc257 100644 --- a/types/inferrawdoctype.d.ts +++ b/types/inferrawdoctype.d.ts @@ -12,7 +12,7 @@ declare module 'mongoose' { ? ObtainSchemaGeneric : FlattenMaps>>; - export type InferRawDocType< + export type InferPojoType< SchemaDefinition, TSchemaOptions extends Record = DefaultSchemaOptions, TTransformOptions = { bufferToBinary: false } @@ -25,6 +25,12 @@ declare module 'mongoose' { : ObtainRawDocumentPathType | null; }, TSchemaOptions>>; + export type InferRawDocType< + SchemaDefinition, + TSchemaOptions extends Record = DefaultSchemaOptions, + TTransformOptions = { bufferToBinary: false } + > = Require_id>; + /** * @summary Allows users to optionally choose their own type for a schema field for stronger typing. * Make sure to check for `any` because `T extends { __rawDocTypeHint: infer U }` will infer `unknown` if T is `any`. From 95a2be74a9736a97416412d75bed49af2445192e Mon Sep 17 00:00:00 2001 From: Phillip Huang Date: Fri, 28 Nov 2025 19:35:53 -0600 Subject: [PATCH 2/2] actually make the change --- types/inferrawdoctype.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/types/inferrawdoctype.d.ts b/types/inferrawdoctype.d.ts index 6c49ccc257..906e457158 100644 --- a/types/inferrawdoctype.d.ts +++ b/types/inferrawdoctype.d.ts @@ -16,14 +16,14 @@ declare module 'mongoose' { SchemaDefinition, TSchemaOptions extends Record = DefaultSchemaOptions, TTransformOptions = { bufferToBinary: false } - > = Require_id = ApplySchemaOptions<{ [ K in keyof (RequiredPaths & OptionalPaths) ]: IsPathRequired extends true ? ObtainRawDocumentPathType : ObtainRawDocumentPathType | null; - }, TSchemaOptions>>; + }, TSchemaOptions>; export type InferRawDocType< SchemaDefinition,