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 - Should LeanDocument<T> include _id? #11118

Closed
Bene-Graham opened this issue Dec 17, 2021 · 3 comments
Closed

TypeScript - Should LeanDocument<T> include _id? #11118

Bene-Graham opened this issue Dec 17, 2021 · 3 comments
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@Bene-Graham
Copy link

I am wondering if LeanDocument should include _id? I am migrating form the @type/mongoose to the built in types but _id is missing.

In the below example _id is print out.

import { Schema, model, connect, LeanDocument } from 'mongoose';

interface User {
    name: string;
    email: string;
    avatar?: string;
}

const schema = new Schema<User>({
    name: { type: String, required: true },
    email: { type: String, required: true },
    avatar: String
});

const UserModel = model<User>('User', schema);

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

async function run(): Promise<void> {
    // 4. Connect to MongoDB
    await connect('CONNECTION STRING HERE');

    await UserModel.create({
        email: "abc@abc.com",
        name: "test"
    });

    const docs = await UserModel.find().lean().exec();

    for(const doc of docs) {
        // @ts-expect-error typing should have _id?????
        console.log(doc._id);
    }
}

What is the expected behavior?

typescript: 4.4.4
node: 16.x
mongoose: 6.1.2

@IslandRhythms IslandRhythms added the can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. label Dec 17, 2021
@IslandRhythms
Copy link
Collaborator

the _id is printing

@Bene-Graham
Copy link
Author

Yes it is printing, but the intelisence says there is no property _id.

If you look at how LeanDocument is defined

  export type LeanDocument<T> = Omit<_LeanDocument<T>, Exclude<keyof Document, '_id' | 'id' | '__v'> | '$isSingleNested'>;

you can see that it is excluding _id from the type.

@IslandRhythms IslandRhythms added typescript Types or Types-test related issue / Pull Request and removed can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. labels Dec 17, 2021
@Bene-Graham
Copy link
Author

digging a bit deeper, lean() does not return a LeanDoument type in my example.

lean<LeanResultType = RawDocType extends Document ? LeanDocumentOrArray<ResultType> : LeanDocumentOrArrayWithRawType<ResultType, RawDocType>>(val?: boolean | any): QueryWithHelpers<LeanResultType, DocType, THelpers, RawDocType>;

lean seems to be taking the path with LeanDocumentOrArrayWithRawType

  export type LeanDocumentOrArrayWithRawType<T, RawDocType> = 0 extends (1 & T) ? T :
    T extends unknown[] ? RawDocType[] :
    T extends Document ? RawDocType :
    T;

Then it seems to take the path of RawDocType[]

I am a bit lost on why there all these branch conditions on the type.

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

No branches or pull requests

3 participants