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

Mongoose 8.7.2 breaks Union Types #15057

Open
2 tasks done
timheerwagen opened this issue Nov 24, 2024 · 0 comments · May be fixed by #15072
Open
2 tasks done

Mongoose 8.7.2 breaks Union Types #15057

timheerwagen opened this issue Nov 24, 2024 · 0 comments · May be fixed by #15072
Labels
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
Milestone

Comments

@timheerwagen
Copy link

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

Node.js version

22

MongoDB server version

6.11

Typescript version (if applicable)

5.7.2

Description

Before 8.7.2, only the type of the union was returned and that was fine. But now the type is returned as binary, but the union has no binaries in it.
It does not matter that the correct union value is not parsed here by mongoose, as this is done by zod.
This error seems to be related to this issue: #14974

Steps to Reproduce

import mongoose, { Model } from "mongoose";

type Attachment =
  | {
      type: "foo";
      value?: undefined;
    }
  | {
      type: "string";
      value?: string;
    };

const TestSchema = new mongoose.Schema<Attachment>({
  type: { type: String, required: true },
  value: { type: String },
});

export const AttachmentModel = mongoose.model<Attachment>("test", TestSchema);

const main = async () => {
  const item = await AttachmentModel.findOne().lean();

  if (!item) return;

  doSomeThing(item);
};

const doSomeThing = (item: Attachment) => {
  console.log(item);
};

doSomeThing returns error
Image

Expected Behavior

No response

@vkarpov15 vkarpov15 added this to the 8.8.4 milestone Nov 26, 2024
@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 Nov 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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
Projects
None yet
2 participants