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 Model#findOne() return type is not as expected #13151

Closed
2 tasks done
s4hubhamp opened this issue Mar 9, 2023 · 0 comments · Fixed by #13155
Closed
2 tasks done

typescript Model#findOne() return type is not as expected #13151

s4hubhamp opened this issue Mar 9, 2023 · 0 comments · Fixed by #13155
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@s4hubhamp
Copy link

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.10.1

Node.js version

16

MongoDB server version

4.2

Typescript version (if applicable)

4.6.3

Description

When I execute findOne method on model I am getting type annotations like below:

image

The problem above is typescript assumes that result will be always there.
So, If latestRowEntry is null I am getting error accessing nested props(latestRowEntry.time).

Ideally, Typescript should not allow the access of nested properties without checking latestRowEntry is null or not.

The annotation that's getting generated is not correct.

Below is my model and it's interface

import {model, Schema, Types} from 'mongoose'

export interface DeviceRawData {
  device_id: string
  date: string
  time: string
  createdAt?: Date | string
  updatedAt?: Date | string
}

export const DeviceRawDataSchema = new Schema(
  {
    device_id: {
      type: String,
      required: true,
    },
    date: {
      type: String,
      required: true,
    },
    time: {
      type: String,
      required: true,
    },
  },
  {
    timestamps: true,
  },
)

const DeviceRawDataModel = model<DeviceRawData>('DeviceRawData', DeviceRawDataSchema)
export default DeviceRawDataModel

Steps to Reproduce

Use below model code and try to query using method model#findOne.

import {model, Schema, Types} from 'mongoose'

export interface DeviceRawData {
  device_id: string
  date: string
  time: string
  createdAt?: Date | string
  updatedAt?: Date | string
}

export const DeviceRawDataSchema = new Schema(
  {
    device_id: {
      type: String,
      required: true,
    },
    date: {
      type: String,
      required: true,
    },
    time: {
      type: String,
      required: true,
    },
  },
  {
    timestamps: true,
  },
)

const DeviceRawDataModel = model<DeviceRawData>('DeviceRawData', DeviceRawDataSchema)
export default DeviceRawDataModel

Expected Behavior

The problem above is typescript assumes that result will be always there.
So, If latestRowEntry is null I am getting error accessing nested props(latestRowEntry.time).

Ideally, Typescript should not allow the access of nested properties without checking latestRowEntry is null or not.
The annotation that's getting generated is not correct.

@vkarpov15 vkarpov15 added the typescript Types or Types-test related issue / Pull Request label Mar 10, 2023
@vkarpov15 vkarpov15 added this to the TypeScript backlog milestone Mar 10, 2023
lpizzinidev added a commit to lpizzinidev/mongoose that referenced this issue Mar 10, 2023
lpizzinidev added a commit to lpizzinidev/mongoose that referenced this issue Mar 13, 2023
vkarpov15 added a commit that referenced this issue Mar 22, 2023
type(query): changed return type of lean() to include null
@vkarpov15 vkarpov15 modified the milestones: TypeScript backlog, 7.0.3 Mar 22, 2023
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.

2 participants