Skip to content

Commit 36fa0c0

Browse files
authored
Merge pull request Automattic#14882 from Automattic/vkarpov15/Automatticgh-14839
type(inferrawdoctype): infer Date types as JS dates rather than Mongoose SchemaType Date
2 parents 286ab98 + 180f5d9 commit 36fa0c0

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

test/types/inferrawdoctype.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { InferRawDocType } from 'mongoose';
2+
import { expectType, expectError } from 'tsd';
3+
4+
function gh14839() {
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 = InferRawDocType< typeof schemaDefinition>;
24+
expectType<{ email: string, password: string, dateOfBirth: Date }>({} as UserType);
25+
}

types/inferrawdoctype.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ declare module 'mongoose' {
9191
IfEquals<PathValueType, String> extends true ? PathEnumOrString<Options['enum']> :
9292
PathValueType extends NumberSchemaDefinition ? Options['enum'] extends ReadonlyArray<any> ? Options['enum'][number] : number :
9393
IfEquals<PathValueType, Schema.Types.Number> extends true ? number :
94-
PathValueType extends DateSchemaDefinition ? Date :
95-
IfEquals<PathValueType, Schema.Types.Date> extends true ? Date :
94+
PathValueType extends DateSchemaDefinition ? NativeDate :
95+
IfEquals<PathValueType, Schema.Types.Date> extends true ? NativeDate :
9696
PathValueType extends typeof Buffer | 'buffer' | 'Buffer' | typeof Schema.Types.Buffer ? Buffer :
9797
PathValueType extends BooleanSchemaDefinition ? boolean :
9898
IfEquals<PathValueType, Schema.Types.Boolean> extends true ? boolean :

types/inferschematype.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,8 @@ type ResolvePathType<PathValueType, Options extends SchemaTypeOptions<PathValueT
281281
IfEquals<PathValueType, String> extends true ? PathEnumOrString<Options['enum']> :
282282
PathValueType extends NumberSchemaDefinition ? Options['enum'] extends ReadonlyArray<any> ? Options['enum'][number] : number :
283283
IfEquals<PathValueType, Schema.Types.Number> extends true ? number :
284-
PathValueType extends DateSchemaDefinition ? Date :
285-
IfEquals<PathValueType, Schema.Types.Date> extends true ? Date :
284+
PathValueType extends DateSchemaDefinition ? NativeDate :
285+
IfEquals<PathValueType, Schema.Types.Date> extends true ? NativeDate :
286286
PathValueType extends typeof Buffer | 'buffer' | 'Buffer' | typeof Schema.Types.Buffer ? Buffer :
287287
PathValueType extends BooleanSchemaDefinition ? boolean :
288288
IfEquals<PathValueType, Schema.Types.Boolean> extends true ? boolean :

0 commit comments

Comments
 (0)