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 error: 'Type instantiation is excessively deep and possibly infinite' for findOneAndUpdate (similar to issue 10189) #10647

Closed
skhilliard opened this issue Aug 31, 2021 · 4 comments
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@skhilliard
Copy link

I would like to report a potential bug. I experienced this compilation error after updating my current Mongoose version from 5.12.8 to 5.13.8

Similar to #10189

Typescript: 4.2.4
Mongoose: 5.13.8
Node: 14.15.4
MongoDB: Azure Cosmos DB

Schema definition (some omissions for brevity):

export interface IRegistrationDocument extends mongoose.Document {
    siteId: ISite | mongoose.Types.ObjectId;
    firstName: string;
    lastName: string;
    sex: Sex;
    dateOfBirth: Date;
    mrn: string;
    mrnSearch: string;
    accountNumber: string;
    visitReason: string;
    arrivalDate: Date;
    merged: boolean;
    address1: string;
    address2: string;
    city: string;
    state: string;
    zip: string;
    homePhone: string;
    workPhone: string;
    email: string;
    pcp: string;
    referringSpecialist: string;
    inboundOrderResults?: any[];
    preferredLanguage?: ILanguageSetting;
    races?: ICodedEntry[];
    ethnicities?: ICodedEntry[];
    primaryInsurance?: IInsuranceSetting;
    secondaryInsurance?: IInsuranceSetting;
    modeOfArrival?: ICodedEntry;
    acuity: string;
    area: string;
    room: string;
    patientFlags?: IPatientFlag[];
    hospitalServiceId: string;
}

export const RegistrationSchema = new mongoose.Schema(
    {
        siteId: { type: mongoose.Schema.Types.ObjectId, ref: 'Site', required: true },
        firstName: { type: String, required: true, default: '' },
        lastName: { type: String, required: true, default: '' },
        sex: { type: Number, default: 0 },
        dateOfBirth: { type: Date, default: Date.now() },
        mrn: { type: String, default: '' },
        mrnSearch: { type: String, uppercase: true, trim: true, default: '' },
        visitReason: { type: String, default: '' },
        accountNumber: { type: String, default: '' },
        arrivalDate: { type: Date, default: Date.now() },
        address1: { type: String, default: '' },
        address2: { type: String, default: '' },
        city: { type: String, default: '' },
        state: { type: String, default: '' },
        zip: { type: String, default: '' },
        homePhone: { type: String, default: '' },
        workPhone: { type: String, default: '' },
        email: { type: String, default: '' },
        referringSpecialist: { type: String, default: '' },
        pcp: { type: String, default: '' },
        merged: { type: Boolean, default: false },
        inboundOrderResults: { type: [inboundOrderResults], default: [] },
        preferredLanguage: { type: PreferredLanguageSchema },
        primaryInsurance: { type: InsuranceSchema, required: false },
        secondaryInsurance: { type: InsuranceSchema, required: false },
        races: { type: [CodedEntrySchema], required: false },
        ethnicities: { type: [CodedEntrySchema], required: false },
        modeOfArrival: { type: CodedEntrySchema, required: false },
        acuity: { type: String, default: '' },
        area: { type: String, default: '' },
        room: { type: String, default: '' },
        patientFlags: { type: [RegistrationPatientFlagSchema], default: [], required: false },
        hospitalServiceId: { type: String }
    },
    {
        timestamps: true,
        shardKey: { siteId: 1 }
    }
);

export const Registration = schemabase.db.model<IRegistrationDocument>('Registration', RegistrationSchema);

Snippet of the code that results in the Typescript compilation error:

                            const registrationRequest = <IRegistrationDocument>{
                                siteId: requestJson.siteId,
                                firstName: requestJson.firstName,
                                lastName: requestJson.lastName,
                                sex: this.mapGender(requestJson.sex),
                                dateOfBirth: this.parseHL7datetime(requestJson.dateOfBirth, timeZone),
                                mrn: requestJson.mrn,
                                mrnSearch: !!requestJson.mrn ? requestJson.mrn.toUpperCase().trim() : '',
                                accountNumber: requestJson.accountNumber,
                                arrivalDate: this.parseHL7datetime(requestJson.arrivalDate, timeZone),
                                visitReason: requestJson.visitReason,
                                address1: requestJson.address1,
                                address2: requestJson.address2,
                                city: requestJson.city,
                                state: requestJson.state,
                                zip: requestJson.zip,
                                homePhone: requestJson.homePhone,
                                workPhone: requestJson.workPhone,
                                email: requestJson.email,
                                referringSpecialist: requestJson.referringSpecialist,
                                pcp: requestJson.pcp,
                                merged: false,
                                preferredLanguage: languageSetting,
                                races: requestJson.races,
                                ethnicities: requestJson.ethnicities,
                                primaryInsurance: requestJson.primaryInsurance,
                                secondaryInsurance: requestJson.secondaryInsurance,
                                modeOfArrival: { code: requestJson.modeOfArrival, description: '' },
                                acuity: requestJson.acuity,
                                area: requestJson.area,
                                room: requestJson.room,
                                patientFlags,
                                hospitalServiceId: requestJson.hospitalServiceId
                            };

                            Registration.findOneAndUpdate(
                                {
                                    siteId: requestJson.siteId,
                                    accountNumber: requestJson.accountNumber
                                },
                                { $set: registrationRequest },
                                {
                                    new: true,
                                    upsert: true
                                }
                            )
                                .then((registration) => {

I did try one of the workarounds suggested in issue 10189. I made IRegistrationDocument no longer extend mongoose.Document. However that didn't fix that compilation error. It actually seemed to confuse Typescript when trying to configure the model (attached screen shot showing this).

#Typescript Warning

Any suggestions?

Thanks,
Kelly

@thaoula
Copy link

thaoula commented Sep 1, 2021

Hi @skhilliard,

Can you try changing the following line from any[] to any -
inboundOrderResults?: any[];

I think the not extending document issue maybe due to your schema not containing _id field. Model looks for _id.

Regards,
Tarek

@skhilliard
Copy link
Author

That change (e.g. changing any[] to any) didn't seem to address the issue; however, after I updated Typescript to version 4.4.2, removing 'extends mongoose.Document' didn't seem to cause the "confusion" when declaring the model.

I think that changing the declarations for these interfaces to not extend Document may be the way to go for fixing this, but it will require a fairly large amount of code updates to get it to compile.

It is looking like the problem is around using the $set operator in findOneAndUpdate where the schema has other nested schemas too.

@IslandRhythms IslandRhythms added the typescript Types or Types-test related issue / Pull Request label Sep 1, 2021
@vkarpov15 vkarpov15 added this to the 6.0.5 milestone Sep 1, 2021
@vkarpov15
Copy link
Collaborator

I've confirmed this issue, it looks like it was introduced in v5.13.8 and is a duplicate of #10617. We'll work on a fix for v5.13.x, but, in the meantime, please downgrade to v5.13.7 or upgrade to v6.0.4, both of which are not affected by this bug.

@vkarpov15 vkarpov15 modified the milestones: 6.0.5, 5.13.9 Sep 2, 2021
@vkarpov15
Copy link
Collaborator

Fix will be in v5.13.9 with b34d1d5. We haven't been able to repro this in our automated tests for some reason, but it happens reliably when running locally with tsc and b34d1d5 fixes it, so we'll ship that in the next 24 hours 👍

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

4 participants