-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,17 @@ | ||
import { Schema, InferSchemaType, SchemaType, SchemaTypeOptions, TypeKeyBaseType, Types, NumberSchemaDefinition, StringSchemaDefinition, BooleanSchemaDefinition, DateSchemaDefinition } from 'mongoose'; | ||
import { | ||
Schema, | ||
InferSchemaType, | ||
SchemaType, | ||
SchemaTypeOptions, | ||
TypeKeyBaseType, | ||
Types, | ||
NumberSchemaDefinition, | ||
StringSchemaDefinition, | ||
BooleanSchemaDefinition, | ||
DateSchemaDefinition, | ||
ObtainDocumentType, | ||
DefaultTypeKey | ||
} from 'mongoose'; | ||
|
||
declare module 'mongoose' { | ||
/** | ||
|
@@ -138,7 +151,8 @@ type ObtainDocumentPathType<PathValueType, TypeKey extends TypeKeyBaseType> = Pa | |
? InferSchemaType<PathValueType> | ||
: ResolvePathType< | ||
PathValueType extends PathWithTypePropertyBaseType<TypeKey> ? PathValueType[TypeKey] : PathValueType, | ||
PathValueType extends PathWithTypePropertyBaseType<TypeKey> ? Omit<PathValueType, TypeKey> : {} | ||
PathValueType extends PathWithTypePropertyBaseType<TypeKey> ? Omit<PathValueType, TypeKey> : {}, | ||
TypeKey | ||
>; | ||
|
||
/** | ||
|
@@ -153,7 +167,7 @@ type PathEnumOrString<T extends SchemaTypeOptions<string>['enum']> = T extends ( | |
* @param {Options} Options Document definition path options except path type. | ||
* @returns Number, "Number" or "number" will be resolved to string type. | ||
*/ | ||
type ResolvePathType<PathValueType, Options extends SchemaTypeOptions<PathValueType> = {}> = | ||
type ResolvePathType<PathValueType, Options extends SchemaTypeOptions<PathValueType> = {}, TypeKey extends TypeKeyBaseType = DefaultTypeKey> = | ||
PathValueType extends Schema ? InferSchemaType<PathValueType> : | ||
PathValueType extends (infer Item)[] ? IfEquals<Item, never, any, ResolvePathType<Item>>[] : | ||
PathValueType extends StringSchemaDefinition ? PathEnumOrString<Options['enum']> : | ||
|
@@ -169,5 +183,5 @@ type ResolvePathType<PathValueType, Options extends SchemaTypeOptions<PathValueT | |
IfEquals<PathValueType, ObjectConstructor> extends true ? any: | ||
IfEquals<PathValueType, {}> extends true ? any: | ||
PathValueType extends typeof SchemaType ? PathValueType['prototype'] : | ||
PathValueType extends {} ? PathValueType : | ||
PathValueType extends Record<string, any> ? ObtainDocumentType<PathValueType, any, TypeKey> : | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
mohammad0-0ahmad
Author
|
||
unknown; |
Hey @mohammad0-0ahmad
This is a pretty lengthy explanation of an issue I'm having that could've been an issue on the mongoose repo, but I prefer to show It to you and if you're getting the issue as well, I'll create a proper issue.
I just updated a project of mine to https://github.com/Automattic/mongoose/releases/tag/6.5.0. You did really good work on the auto-typed virtuals!!
This is the schema, and it is auto-typed like I expected it to, when I hover over it,
But when I make a model with the schema above, it's not as I expected and it's the same with the
InferSchemaType
helper.I found this to be a very strange issue because the
Schema.Types.ObjectId
should've matched above and resulted in aTypes.ObjectId
type. So I reverted the change you made in 3feb8af and hardcoded the types anObjectId
is meant to match to and it worked, giving me the correct model definition.This was the change I made. On https://github.com/Automattic/mongoose/blob/3feb8af37c4d65bdc2e68b230882723c33cdfe7c/types/inferschematype.d.ts#L180
This change was already weird to me because the
ObjectIdSchemaDefinition
and the type before are virtually the same, with only the casing of one of the strings (which I didn't use) that was changed.I kept on trying, and found another change that solved the issue for me.
It was with
mongoose/types/inferschematype.d.ts
Line 186 in d092fdc
ObtainDocumentType
to infer the types of nested objects. From the Automattic#12030 issue. I don't know why this is thee case, because the ObjectId should've been matched lines before this one so it wouldn't have to bee deeply checked for other schema types.