Skip to content

Commit

Permalink
type(inferrawdoctype): infer Date types as JS dates rather than Mongo…
Browse files Browse the repository at this point in the history
…ose SchemaType Date

Fix #14839
  • Loading branch information
vkarpov15 committed Sep 10, 2024
1 parent 286ab98 commit 180f5d9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
25 changes: 25 additions & 0 deletions test/types/inferrawdoctype.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { InferRawDocType } from 'mongoose';
import { expectType, expectError } from 'tsd';

function gh14839() {
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 = InferRawDocType< typeof schemaDefinition>;
expectType<{ email: string, password: string, dateOfBirth: Date }>({} as UserType);
}
4 changes: 2 additions & 2 deletions types/inferrawdoctype.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ declare module 'mongoose' {
IfEquals<PathValueType, String> extends true ? PathEnumOrString<Options['enum']> :
PathValueType extends NumberSchemaDefinition ? Options['enum'] extends ReadonlyArray<any> ? Options['enum'][number] : number :
IfEquals<PathValueType, Schema.Types.Number> extends true ? number :
PathValueType extends DateSchemaDefinition ? Date :
IfEquals<PathValueType, Schema.Types.Date> extends true ? Date :
PathValueType extends DateSchemaDefinition ? NativeDate :
IfEquals<PathValueType, Schema.Types.Date> extends true ? NativeDate :
PathValueType extends typeof Buffer | 'buffer' | 'Buffer' | typeof Schema.Types.Buffer ? Buffer :
PathValueType extends BooleanSchemaDefinition ? boolean :
IfEquals<PathValueType, Schema.Types.Boolean> extends true ? boolean :
Expand Down
4 changes: 2 additions & 2 deletions types/inferschematype.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,8 @@ type ResolvePathType<PathValueType, Options extends SchemaTypeOptions<PathValueT
IfEquals<PathValueType, String> extends true ? PathEnumOrString<Options['enum']> :
PathValueType extends NumberSchemaDefinition ? Options['enum'] extends ReadonlyArray<any> ? Options['enum'][number] : number :
IfEquals<PathValueType, Schema.Types.Number> extends true ? number :
PathValueType extends DateSchemaDefinition ? Date :
IfEquals<PathValueType, Schema.Types.Date> extends true ? Date :
PathValueType extends DateSchemaDefinition ? NativeDate :
IfEquals<PathValueType, Schema.Types.Date> extends true ? NativeDate :
PathValueType extends typeof Buffer | 'buffer' | 'Buffer' | typeof Schema.Types.Buffer ? Buffer :
PathValueType extends BooleanSchemaDefinition ? boolean :
IfEquals<PathValueType, Schema.Types.Boolean> extends true ? boolean :
Expand Down

0 comments on commit 180f5d9

Please sign in to comment.