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

mongoose\types\connection.d.ts has incorrect definition for model #12129

Closed
2 tasks done
bradleysimard opened this issue Jul 20, 2022 · 1 comment · Fixed by #12240
Closed
2 tasks done

mongoose\types\connection.d.ts has incorrect definition for model #12129

bradleysimard opened this issue Jul 20, 2022 · 1 comment · Fixed by #12240
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@bradleysimard
Copy link

Prerequisites

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

Issue

I'm using mongoose 6.4.5, for reference.

When defining a model directly against the global mongoose reference, the type checking works perfectly fine. Here's an example lifted right out of your documentation:

const kittySchema = new mongoose.Schema({
        name: String
    });

const Kitten = mongoose.model('Kitten', kittySchema);
const fluffy = new Kitten({ name: 'fluffy' });
await fluffy.save();

If I look at the fluffy instance, I can properly see my schema:
image

I can also see the definition of the .save() method:
image

Now, when I'm trying to define a model against a specific connection (which to me, seems like a more likely situation people would be using this), the type checking does not work properly. Here's an example based on the first example:

const connection = await mongoose.createConnection(`mongodb://localhost:27017`).asPromise();
const doggySchema = new mongoose.Schema({
    name: String
});
const Doggy = connection.model('Doggy', doggySchema);
const barky = new Doggy({ name: 'barky' });
await barky.save();

If I look at my barky instance, my schema is not visible (type is "any"):
image

Which also means that the definition of the .save() method is also "any":
image

My current workaround is to take the type definition of model from node_modules\mongoose\types\index.d.ts and replace the definition of model in mongoose\types\connection.d.ts. This makes it work, but I'm sure it's not the proper way to do it.

@hasezoey
Copy link
Collaborator

The reason probably is that the model type for connection is still the old "plain" type, not the updated type from PR #11563

@IslandRhythms IslandRhythms added the typescript Types or Types-test related issue / Pull Request label Jul 20, 2022
@vkarpov15 vkarpov15 added this to the 6.4.8 milestone Jul 23, 2022
@vkarpov15 vkarpov15 modified the milestones: 6.5.1, 6.5.2 Aug 3, 2022
hasezoey added a commit to hasezoey/mongoose that referenced this issue Aug 9, 2022
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 a pull request may close this issue.

4 participants