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

[Typescript] Schema ref property type declaration issue #13672

Closed
2 tasks done
Jule- opened this issue Jul 29, 2023 · 3 comments
Closed
2 tasks done

[Typescript] Schema ref property type declaration issue #13672

Jule- opened this issue Jul 29, 2023 · 3 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. typescript Types or Types-test related issue / Pull Request

Comments

@Jule-
Copy link

Jule- commented Jul 29, 2023

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

6.6.0

Node.js version

18.15.0

MongoDB server version

5.0.11

Typescript version (if applicable)

5.1.6

Description

There is an issue when defining cross-database ref in a Schema where the Model has TVirtuals type provided.

error TS2322: Type '{ type: typeof Schema.Types.ObjectId; ref: ModelA; }' is not assignable to type 'SchemaDefinitionProperty<ObjectId> | undefined'.
  Types of property 'ref' are incompatible.
    Type 'ModelA' is not assignable to type 'string | Model<any, {}, {}, {}, any> | ((this: any, doc: any) => string | Model<any, {}, {}, {}, any>) | undefined'.
      Type 'Model<IModelA, object, object, IModelAVirtuals, any>' is not assignable to type 'Model<any, {}, {}, {}, any>'.
        Property 'virtualProperty' is missing in type '{}' but required in type 'IModelAVirtuals'.

Steps to Reproduce

// repro.ts
import { model, Model, Schema, Types } from "mongoose";

interface IModelA {
  name: string;
}

interface IModelAVirtuals {
  readonly virtualProperty: string;
}

type ModelA = Model<IModelA, object, object, IModelAVirtuals>;

const modelASchema = new Schema<IModelA, ModelA, object, object, IModelAVirtuals>({
  name: String,
});

modelASchema.virtual("virtualProperty").get(function () {
  return "Virtual!";
});

const A = model<IModelA, ModelA>("ModelA", modelASchema);

interface IModelB {
  a: Types.ObjectId;
}

const modelBSchema = new Schema<IModelB>({
  // Here we use the Model instance for cross-database population
  // https://mongoosejs.com/docs/populate.html#cross-db-populate
  a: { type: Schema.Types.ObjectId, ref: A },
});

const B = model<IModelB>("ModelB", modelBSchema);
npx ts-node repro.ts

Expected Behavior

The ref type declaration should allow virtuals to be present in the Model type.

I think modifying these 2 lines should be enough:

ref?: string | Model<any> | ((this: any, doc: any) => string | Model<any>);

ref(ref: string | boolean | Model<any>): this;

Not sure if this type could be inferred somewhere or if it should only be Model<any, any, any, any>.

@vkarpov15 vkarpov15 added this to the 7.4.3 milestone Aug 1, 2023
@vkarpov15 vkarpov15 added has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue typescript Types or Types-test related issue / Pull Request labels Aug 1, 2023
@IslandRhythms IslandRhythms added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels Aug 2, 2023
@IslandRhythms
Copy link
Collaborator

image
This is not occuring on mongoose 7.4.1 so bug is on the described version only

@Jule-
Copy link
Author

Jule- commented Aug 3, 2023

@IslandRhythms oh yes you are right! So ignore my comments about a potential fix then, it should exist a better solution.

@vkarpov15
Copy link
Collaborator

This was fixed in 6.9.3, likely with #13066. Upgrade to >= 6.9.3.

@vkarpov15 vkarpov15 removed this from the 7.4.3 milestone Aug 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. typescript Types or Types-test related issue / Pull Request
Projects
None yet
Development

No branches or pull requests

3 participants