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 - .lean output is missing types #15189

Closed
2 tasks done
BB-19 opened this issue Jan 20, 2025 · 3 comments
Closed
2 tasks done

Typescript - .lean output is missing types #15189

BB-19 opened this issue Jan 20, 2025 · 3 comments
Labels
can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. typescript Types or Types-test related issue / Pull Request

Comments

@BB-19
Copy link

BB-19 commented Jan 20, 2025

Prerequisites

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

Mongoose version

8.9.5

Node.js version

23.5.0

MongoDB server version

6

Typescript version (if applicable)

5.7.2

Description

There has been a breaking change that causes an invalid type with the .lean output.
Previously, the types were properly inherited but this broke after 8.6.4. The issue still exists with the newest mongoose version 8.9.5.

Last working version: 8.6.4

See example to reproduce.

Steps to Reproduce

  1. Modify Typescript example from Mongoose docs as follows:
import { Schema, model, connect } from 'mongoose';

// 1. Create an interface representing a document in MongoDB.
interface IUser {
   name: string;
   email: string;
   avatar?: string;
}

// 2. Create a Schema corresponding to the document interface.
const userSchema = new Schema<IUser>({
   name: { type: String, required: true },
   email: { type: String, required: true },
   avatar: String,
});

// 3. Create a Model.
const User = model<IUser>('User', userSchema);

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

async function run() {
   // 4. Connect to MongoDB
   await connect('mongodb://127.0.0.1:27017/test');

   const queriedUser = await User.findOne({ name: 'Bill' }).exec();
   queriedUser?.email // string

   const queriedUserLean = await User.findOne({ name: 'Bill' }).lean();
   queriedUserLean?.email // error in newer mongoose versions (but works in 8.6.4)
}
  1. Test with 8.6.4 -> No error
  2. Test with newer mongoose -> Type error when using .lean()

Expected Behavior

Types should be inherited like in 8.6.4 when using .lean()

@vkarpov15 vkarpov15 added this to the 8.9.6 milestone Jan 20, 2025
@vkarpov15 vkarpov15 added has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue typescript Types or Types-test related issue / Pull Request labels Jan 20, 2025
@bartlomiejwulff
Copy link

I‘m facing the same issue

@vkarpov15 vkarpov15 modified the milestones: 8.9.6, 8.9.7 Jan 31, 2025
@vkarpov15
Copy link
Collaborator

I'm unable to repro this, TypeScript playground shows no errors: https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAbzgZQMYAsCmICGAaOECAE0wBsDUIA7azVeAXzgDMoIQ4ByI6gcwgQAzpi4BuAFASA9NLgBGAHRwAwlEw4YmODmpxg1LVBY5U29WHUjDBvjrjEIqAK4hMh-XoCyNAQBEAIUUJAyMTMzgASQBVEShECTgk6hw3AC44IRgoW0kkuGwcYDIMrJz+PKScADdNHCgAflLs3IlGKVk4ACZlNQ0tezQsXDgqKCtIamJbOBgIWawHJ1d3eFDMY1NMYKpqLLhnOKHCuABeODoAdxQMQoAeGLiAPgAKBETk1MwMpBgATzA3xQLX4BHUAEdnMB1MQMtlnNpGHgPgVcMUfrMAUDkCC+GDMJDoZhYbMoAi4EiUTU6lAMjjyni2gBKSQyOQAZl66k02hwcB8pDIOxo+1iGzOhBI5AeYqgry4sq4BEOG2OuBZUjJ1BeTMUqE0GBeLw2UCZZyeoxFEDI2zIED4xvGTI1EhwQj+1FQrGcnpgwBocC1OoS+U6ABZejQ6AxZvMfPwIIEqZcivBdtGYC8eL4SAAjNKyeRdADsigADOXFPI0iWy-Ji9ItFkuC78rt9pCNsBibKJTgU8B4LLFCwDMQAPJ0N4XL4ZLgBYpkLgU3WYAAe9B1lTgnZyPbiDUUhWKcE6ZVsUjbIvgu+7xFlABkNHpzv3U3Bh6OppPMNOUuluAXMglxXRQbV0LcUVvfcNifXRD2PMhTzkE1oE8C5MEucVeAEYRtGqDYhH9PY4BeXNnHgS5oAAayEdCAA5FAANkUMMmTaIA and running locally doesn't have any errors either

$ ./node_modules/.bin/tsc --strict gh-15189.ts 
$ head ./node_modules/typescript/package.json 
{
    "name": "typescript",
    "author": "Microsoft Corp.",
    "homepage": "https://www.typescriptlang.org/",
    "version": "5.7.3",
    "license": "Apache-2.0",
    "description": "TypeScript is a language for application scale JavaScript development",
    "keywords": [
        "TypeScript",
        "Microsoft",
$ 

Can you please provide more detailed repro instructions, like what your tsconfig.json looks like and how you're running tsc?

@vkarpov15 vkarpov15 added can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels Feb 1, 2025
@BB-19
Copy link
Author

BB-19 commented Feb 3, 2025

Seems like this is happening when the tsconfig is missing "node" under compilerOptions.types.
Not sure why it worked in the past and why it works with .exec with the same tsconfig but issue was solved for me by adding the node types.

Closing issue. Feel free to re-open if this is a bug.

@BB-19 BB-19 closed this as completed Feb 3, 2025
@vkarpov15 vkarpov15 removed this from the 8.9.7 milestone Feb 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. typescript Types or Types-test related issue / Pull Request
Projects
None yet
Development

No branches or pull requests

3 participants