Skip to content
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

docs: add brief note on TypeScript generic usage for embedded discriminator path() calls #13728

Merged
merged 1 commit into from Aug 13, 2023

Conversation

vkarpov15
Copy link
Collaborator

Re: #10435

Summary

Added back embedded discriminator docs, with a couple of comments on path() generic requirement for TypeScript.

I'll take another look and see if we can maybe remove the need to do schema.path<Schema.Types.Subdocument> and just have Mongoose infer the type correctly.

Examples

@hasezoey hasezoey added the typescript Types or Types-test related issue / Pull Request label Aug 12, 2023
@vkarpov15
Copy link
Collaborator Author

No luck on getting TypeScript to automatically infer the schematype from path() calls. The closest I got was the following:

type SchemaTypeFromType<T> = T extends Types.DocumentArray<any> ? Schema.Types.DocumentArray : SchemaType<T>;

    path<pathGeneric extends keyof IfAny<EnforcedDocType, {}>>(path: pathGeneric): SchemaType<IfAny<EnforcedDocType, {}>[pathGeneric]>;
    path<pathGeneric extends keyof IfAny<DocType, {}>>(path: pathGeneric): SchemaTypeFromType<IfAny<DocType, {}>[pathGeneric]>;

However, with those changes, I get a bunch of test failures like the following:

  ✖  130:2  Type Schema<IProfile, Model<IProfile, {}, {}, {}, Document<unknown, {}, IProfile> & IProfile & { _id: ObjectId; }, any>, ... 6 more ..., Document<...> & ... 1 more ... & { ...; }> is not assignable to type SchemaDefinitionProperty<IProfile> | undefined.
  Type Schema<IProfile, Model<IProfile, {}, {}, {}, Document<unknown, {}, IProfile> & IProfile & { _id: ObjectId; }, any>, ... 6 more ..., Document<...> & ... 1 more ... & { ...; }> is not assignable to type Schema<any, any, any, {}, {}, {}, DefaultSchemaOptions, { [x: string]: any; }, Document<unknown, {}, { [x: string]: any; }> & { [x: string]: any; } & Required<{ _id: unknown; }>>.
    Types of property obj are incompatible.
      Type { age?: SchemaDefinitionProperty<number> | undefined; } is not assignable to type { [path: string]: SchemaDefinitionProperty<undefined>; } | { [x: string]: SchemaDefinitionProperty<any> | undefined; }.
        Type { age?: SchemaDefinitionProperty<number> | undefined; } is not assignable to type { [x: string]: SchemaDefinitionProperty<any> | undefined; }.
          Property age is incompatible with index signature.
            Type SchemaDefinitionProperty<number> is not assignable to type SchemaDefinitionProperty<any> | undefined. 

Seems like accessing keyof DocType in a generic throws TypeScript off. I'm just going to merge this PR, and take a look at getting TypeScript to correctly infer schema types when calling path() another time.

@vkarpov15 vkarpov15 merged commit b3e91e3 into master Aug 13, 2023
33 of 34 checks passed
@vkarpov15 vkarpov15 added this to the 7.4.4 milestone Aug 13, 2023
@AbdelrahmanHafez AbdelrahmanHafez deleted the vkarpov15/gh-10435 branch August 13, 2023 23:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
typescript Types or Types-test related issue / Pull Request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants