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

Cursor.next can return null, but TypeScript says it can't #14787

Closed
2 tasks done
lantw44 opened this issue Aug 2, 2024 · 0 comments · Fixed by #14798
Closed
2 tasks done

Cursor.next can return null, but TypeScript says it can't #14787

lantw44 opened this issue Aug 2, 2024 · 0 comments · Fixed by #14798
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@lantw44
Copy link
Contributor

lantw44 commented Aug 2, 2024

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.13.0, 7.6.13, 8.5.2

Node.js version

20.12.2

MongoDB server version

6.0.16

Typescript version (if applicable)

5.5.4

Description

┌──────────────────────────────────────────────────────────────────────────────────────────┐
│(method) Cursor<DocType = any, Options = never>.next(): Promise<DocType>                  │
│                                                                                          │
│Get the next document from this cursor. Will return null when there are                   │
│no documents left.                                                                        │
└──────────────────────────────────────────────────────────────────────────────────────────┘

The Cursor.next method is documented to return null sometimes, but TypeScript thinks it always returns non-null.

Steps to Reproduce

import mongoose from 'mongoose';

const helloSchema = new mongoose.Schema({
  name: { type: String, required: true },
  status: { type: Boolean, required: true },
});
const Hello = mongoose.model('Hello', helloSchema);

const cursor = Hello.find().cursor();
let doc = await cursor.next();
doc = null;

It failed to compile:

mongoose-ts.mts:11:1 - error TS2322: Type 'null' is not assignable to type 'Document<unknown, {}, { name: string; status: boolean; }> & { name: string; status: boolean; } & { _id: ObjectId; }'.
  Type 'null' is not assignable to type 'Document<unknown, {}, { name: string; status: boolean; }>'.

11 doc = null;
   ~~~

It also caused code like this to be flagged by TypeScript ESLint @typescript-eslint/no-unnecessary-condition rule:

for (let doc = await cursor.next(); doc != null; doc = await cursor.next()) {

Expected Behavior

It should compile. doc could be null, so TypeScript should allow assigning null to it.

@IslandRhythms IslandRhythms added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. typescript Types or Types-test related issue / Pull Request labels Aug 7, 2024
@vkarpov15 vkarpov15 added this to the 8.5.3 milestone Aug 8, 2024
@vkarpov15 vkarpov15 removed the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Aug 8, 2024
vkarpov15 added a commit that referenced this issue Aug 10, 2024
types(cursor): indicate that cursor.next() can return null
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.

3 participants