Skip to content

Projections now requiring use of as const. Type 'number' is not assignable to type 'boolean | 0 | 1' #15557

@thaoula

Description

@thaoula

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

Node.js version

v24.1.0

MongoDB server version

8.x

Typescript version (if applicable)

5.8.3

Description

Hi Team,

I just upgraded our very large project from 8.14.1 -> 8.17.0 and we are encountering build errors due to Mongoose type changes.

Previously code like the following worked fine.

 const query = {
     active: true,
};

const projection = {
    _id: 1,
};

const job = await this.jobDocument
    .findOne(query, projection)
    .lean()
    .exec() as Job;

Now we are getting build errors because due the projection being invalid. Changing the projection as follows resolves the issue.

const projection = {
    _id: 1 as const,
};

This change has generated 100's of build errors and makes writing the projection more verbose as now we have to add as const each time we add a field for projection.

Is there are global way to override this?

Steps to Reproduce

Create a query as described in the description.

Expected Behavior

The following projection works just like previously

const projection = {
    _id: 1,
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions