Skip to content
Open
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
26 changes: 24 additions & 2 deletions test/types/inferrawdoctype.test.ts
Original file line number Diff line number Diff line change
@@ -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: {
Expand Down
12 changes: 9 additions & 3 deletions types/inferrawdoctype.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,24 @@ declare module 'mongoose' {
? ObtainSchemaGeneric<TSchema, 'EnforcedDocType'>
: FlattenMaps<SubdocsToPOJOs<ObtainSchemaGeneric<TSchema, 'DocType'>>>;

export type InferRawDocType<
export type InferPojoType<
SchemaDefinition,
TSchemaOptions extends Record<any, any> = DefaultSchemaOptions,
TTransformOptions = { bufferToBinary: false }
> = Require_id<ApplySchemaOptions<{
> = ApplySchemaOptions<{
[
K in keyof (RequiredPaths<SchemaDefinition, TSchemaOptions['typeKey']> &
OptionalPaths<SchemaDefinition, TSchemaOptions['typeKey']>)
]: IsPathRequired<SchemaDefinition[K], TSchemaOptions['typeKey']> extends true
? ObtainRawDocumentPathType<SchemaDefinition[K], TSchemaOptions['typeKey'], TTransformOptions>
: ObtainRawDocumentPathType<SchemaDefinition[K], TSchemaOptions['typeKey'], TTransformOptions> | null;
}, TSchemaOptions>>;
}, TSchemaOptions>;

export type InferRawDocType<
SchemaDefinition,
TSchemaOptions extends Record<any, any> = DefaultSchemaOptions,
TTransformOptions = { bufferToBinary: false }
> = Require_id<InferPojoType<SchemaDefinition, TSchemaOptions, TTransformOptions>>;

/**
* @summary Allows users to optionally choose their own type for a schema field for stronger typing.
Expand Down