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 doc exemple for query helpers does not seem to work #11709

Closed
MathieuFouillet opened this issue Apr 22, 2022 · 1 comment
Closed
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@MathieuFouillet
Copy link

Do you want to request a feature or report a bug?

It's either a bug or an issue with the doc.

What is the current behavior?

I can't create the simple query helper from the doc. I have these 2 errors :

  • TS2339: Property 'byName' does not exist on type '{}'.
  • TS2339: Property 'find' does not exist on type '{}'.

import { Document, Model, Query, Schema, connect, model } from 'mongoose';

interface Project {
  name: string;
  stars: number;
}

const schema = new Schema<Project>({
  name: { type: String, required: true },
  stars: { type: Number, required: true }
});
// Query helpers should return `Query<any, Document<DocType>> & ProjectQueryHelpers`
// to enable chaining.
interface ProjectQueryHelpers {
  byName(name: string): Query<any, Document<Project>> & ProjectQueryHelpers;
}
schema.query.byName = function(name): Query<any, Document<Project>> & ProjectQueryHelpers {
  return this.find({ name: name });
};

// 2nd param to `model()` is the Model class to return.
const ProjectModel = model<Project, Model<Project, ProjectQueryHelpers>>('Project', schema);

run().catch(err => console.log(err));

async function run(): Promise<void> {
  await connect('mongodb://localhost:27017/test');
  
  // Equivalent to `ProjectModel.find({ stars: { $gt: 1000 }, name: 'mongoose' })`
  await ProjectModel.find().where('stars').gt(1000).byName('mongoose');
}

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.

  • node v14.15.4
  • "typescript": "^4.6.3"
  • "mongoose": "^6.3.1"
  • mongodb: 4.4.6
  • tsconfig.json
{
  "$schema": "https://json.schemastore.org/tsconfig",
  "display": "Node 14",

  "compilerOptions": {
    "lib": ["es2020"],
    "module": "commonjs",
    "target": "es2020",

    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  }
}
@IslandRhythms IslandRhythms added the typescript Types or Types-test related issue / Pull Request label Apr 26, 2022
@IslandRhythms
Copy link
Collaborator

Does not happen on 6.3.0

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

No branches or pull requests

3 participants