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

Strictly typed Query Cursor async iterator #10252

Closed
borfig opened this issue May 15, 2021 · 0 comments · Fixed by #10253
Closed

Strictly typed Query Cursor async iterator #10252

borfig opened this issue May 15, 2021 · 0 comments · Fixed by #10253

Comments

@borfig
Copy link
Contributor

borfig commented May 15, 2021

Do you want to request a feature or report a bug?
IMHO, it is a feature.

What is the current behavior?
When using for await with query cursors, the returned objects are of type any, which can hide possible bugs.

The following buggy TypeScript code compiles, and the bug will be discovered only in runtime:

import { Schema, model } from "mongoose";
interface Test {
  test: string;
}
const schema = new Schema<Test>({
  test: { type: String, required: true },
});
const TestModel = model<Test>("Test", schema);
async function main() {
  const cursor = TestModel.find({}).cursor();
  for await (const test of cursor) {
    console.log(test.tedt); // intentional bug
  }
}
main()

My tsconfig.json:

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es2019",
    "lib": [
      "dom",
      "es2019",
      "es2020.bigint",
      "es2020.string",
      "es2020.symbol.wellknown",
    ],
    "esModuleInterop": true,
    "strict": true,
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "sourceMap": true,
    "outDir": "dist",
    "baseUrl": ".",
    "jsx": "react"
  },
  "include": [
    "src/**/*.ts*",
  ]
}

What is the expected behavior?
I am proposing to change the QueryCursor's [Symbol.asyncIterator] to return a strictly-typed AsyncIterableIterator<DocType>, instead of the inherited AsyncIterableIterator<any>

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

  • Mongoose v5.12.9
  • Node.js v12.22.1
  • MongoDB v4.4.6
  • TypeScript v4.2.4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant