-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix types resolved from InferSchemaType #12450
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change seems to break some existing types tests (see test runs), could you maybe fix those and add new ones that this change is meant to address?
Note: in case you dont know how to run those types tests locally:
- install everything with
npm install
- and run the types test with
npm run test-tsd
Yes, I'm testing right now |
1c08486
to
3cb7ec7
Compare
I've rebased on top of master, but I have an issue that I don't know how to fix |
The following would fix it for now as mentioned in #12431 (comment), though i dont know why it happens (probably a typescript bug?) index 7c0038a869..ff9914217c 100644
--- a/types/inferschematype.d.ts
+++ b/types/inferschematype.d.ts
@@ -38,7 +38,16 @@ declare module 'mongoose' {
* // result
* type UserType = {userName?: string}
*/
- type InferSchemaType<SchemaType> = ObtainSchemaGeneric<SchemaType, 'DocType'>;
+ type InferSchemaType<SchemaType> = ObtainDocType<SchemaType>; // "ObtainDocType" is used over "ObtainSchemaGeneric", because "ObtainSchemaGeneric" would return a modified result
+
+ /**
+ * @summary Obtains the "DocType" generic of a Schema, has to be used because with "ObtainSchemaGeneric" it would return a modified type
+ * @param {TSchema} TSchema A generic of schema type instance
+ */
+ type ObtainDocType<TSchema> =
+ TSchema extends Schema<any, any, any, any, any, any, any, infer DocType>
+ ? DocType
+ : unknown;
/**
* @summary Obtains schema Generic type by using generic alias. |
I've applied your changes @hasezoey but have failing tests |
i have updated my comment at #12431 (comment) because it (after some more testing) contained "wrong" information. it seems like if we dont do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suspect this will be fixed by #12352, I'll check a little later.
* @param {TSchema} TSchema A generic of schema type instance | ||
*/ | ||
type ObtainDocType<TSchema> = | ||
TSchema extends Schema<any, any, any, any, any, any, any, infer DocType> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't do this. For some reason, not inferring all the generics (there's one in particular that is important that I forget) breaks stuff. There's a little more info on this PR: #12352 . I don't know why, TypeScript is filled with delightful little surprises like that.
Summary
Currently
InferSchemaType<>
returns weird types when usingSchema.Types.*
, check #12431 for better explanationExamples