Skip to content

Commit 505d6a0

Browse files
Export InferPojoType to replicate Mongoose 8 InferRawDocType behavior
1 parent 70b5a30 commit 505d6a0

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

test/types/inferrawdoctype.test.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,28 @@
1-
import { InferRawDocType, type InferSchemaType, type ResolveTimestamps, type Schema, type Types } from 'mongoose';
2-
import { expectType, expectError } from 'tsd';
1+
import { InferRawDocType, type InferPojoType, type ResolveTimestamps, type Schema, type Types } from 'mongoose';
2+
import { expectType } from 'tsd';
33

4+
function inferPojoType() {
5+
const schemaDefinition = {
6+
email: {
7+
type: String,
8+
trim: true,
9+
required: true,
10+
unique: true,
11+
lowercase: true
12+
},
13+
password: {
14+
type: String,
15+
required: true
16+
},
17+
dateOfBirth: {
18+
type: Date,
19+
required: true
20+
}
21+
};
22+
23+
type UserType = InferPojoType< typeof schemaDefinition>;
24+
expectType<{ email: string, password: string, dateOfBirth: Date }>({} as UserType);
25+
}
426
function gh14839() {
527
const schemaDefinition = {
628
email: {

types/inferrawdoctype.d.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ declare module 'mongoose' {
1212
? ObtainSchemaGeneric<TSchema, 'EnforcedDocType'>
1313
: FlattenMaps<SubdocsToPOJOs<ObtainSchemaGeneric<TSchema, 'DocType'>>>;
1414

15-
export type InferRawDocType<
15+
export type InferPojoType<
1616
SchemaDefinition,
1717
TSchemaOptions extends Record<any, any> = DefaultSchemaOptions,
1818
TTransformOptions = { bufferToBinary: false }
@@ -25,6 +25,12 @@ declare module 'mongoose' {
2525
: ObtainRawDocumentPathType<SchemaDefinition[K], TSchemaOptions['typeKey'], TTransformOptions> | null;
2626
}, TSchemaOptions>>;
2727

28+
export type InferRawDocType<
29+
SchemaDefinition,
30+
TSchemaOptions extends Record<any, any> = DefaultSchemaOptions,
31+
TTransformOptions = { bufferToBinary: false }
32+
> = Require_id<InferPojoType<SchemaDefinition, TSchemaOptions, TTransformOptions>>;
33+
2834
/**
2935
* @summary Allows users to optionally choose their own type for a schema field for stronger typing.
3036
* Make sure to check for `any` because `T extends { __rawDocTypeHint: infer U }` will infer `unknown` if T is `any`.

0 commit comments

Comments
 (0)