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: Mongoose lean does not return a real lean document for Array subschema #13830

Closed
2 tasks done
GuiHash opened this issue Sep 6, 2023 · 1 comment
Closed
2 tasks done

Comments

@GuiHash
Copy link

GuiHash commented Sep 6, 2023

Prerequisites

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

Mongoose version

7.x

Node.js version

all

MongoDB server version

all

Typescript version (if applicable)

all

Description

Mongoose does not return a real lean document

Since v7 and the removal of LeanDocument, now when we lean a document mongoose return the raw type defined when we instantiate the schema

But when we are not providing a raw type to the schema and we let mongoose infer the type the returned type is not the good one (for instance for subSchema array)

Steps to Reproduce

import mongoose, { Schema } from "mongoose";

const subSchema = new Schema({ name: String });

const schema = new Schema({
  sub: { type: [subSchema] },
});

const Model = mongoose.model("Model", schema);

const c = await Model.findById("my-id").lean();

c;
// ^? const c: (mongoose.FlattenMaps<{ sub: mongoose.Types.DocumentArray<{ name?: string | undefined }> }> & { _id: mongoose.Types.ObjectId }) | null;

The return type for the subSchema is mongoose.Types.DocumentArray

Typescript playground example

Expected Behavior

import mongoose, { Schema } from "mongoose";

const subSchema = new Schema({ name: String });

const schema = new Schema({
  sub: { type: [subSchema] },
});

const Model = mongoose.model("Model", schema);

const c = await Model.findById("my-id").lean();

c;
// ^? const c: (mongoose.FlattenMaps<{ sub: Array<{ name?: string | undefined }> }> & { _id: mongoose.Types.ObjectId }) | null;

The return type for the subSchema is Array

@GuiHash
Copy link
Author

GuiHash commented Sep 6, 2023

Sorry after more research it is a duplicate of #13772

@GuiHash GuiHash closed this as completed Sep 6, 2023
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

No branches or pull requests

1 participant