Automattic / mongoose

MongoDB object modeling designed to work in an asynchronous environment.
https://mongoosejs.com
MIT License
27.01k stars 3.85k forks source link

Types break in semver patch change #14462

Closed Anonymous4078 closed 7 months ago

Anonymous4078 commented 8 months ago

Prerequisites

Mongoose version

8.2.3

Node.js version

20.11.1

MongoDB server version

5.x

Typescript version (if applicable)

5.4.3

Description

› yarn build
Error: src/listeners/message/messageBulkDelete.ts(28,49): error TS2769: No overload matches this call.
  Overload [1](https://github.com/Anonymous4078/suggester/actions/runs/8382964808/job/22957771088?pr=327#step:6:1) of [4](https://github.com/Anonymous4078/suggester/actions/runs/8382964808/job/22957771088?pr=327#step:6:5), '(filter?: FilterQuery<GuildConfig> | undefined, projection?: ProjectionType<GuildConfig> | null | undefined, options?: QueryOptions<...> | ... 1 more ... | undefined): Query<...>', gave the following error.
    Type 'string | null' is not assignable to type 'Condition<string> | undefined'.
      Type 'null' is not assignable to type 'Condition<string> | undefined'.
  Overload 2 of 4, '(filter?: FilterQuery<GuildConfig> | undefined, projection?: ProjectionType<GuildConfig> | null | undefined): Query<...>', gave the following error.
    Type 'string | null' is not assignable to type 'Condition<string> | undefined'.
  Overload 3 of 4, '(filter?: FilterQuery<GuildConfig> | undefined): Query<(Document<unknown, {}, GuildConfig> & GuildConfig & { _id: ObjectId; }) | null, Document<...> & ... 1 more ... & { ...; }, {}, GuildConfig, "findOne">', gave the following error.
    Type 'string | null' is not assignable to type 'Condition<string> | undefined'.
Error: src/listeners/message/messageBulkDelete.ts(32,1[5](https://github.com/Anonymous4078/suggester/actions/runs/8382964808/job/22957771088?pr=327#step:6:6)): error TS2322: Type 'string | null' is not assignable to type 'Condition<string> | undefined'.
Error: src/listeners/message/messageDelete.ts(21,45): error TS2769: No overload matches this call.
  Overload 1 of 4, '(filter?: FilterQuery<GuildConfig> | undefined, projection?: ProjectionType<GuildConfig> | null | undefined, options?: QueryOptions<...> | ... 1 more ... | undefined): Query<...>', gave the following error.
    Type 'string | null' is not assignable to type 'Condition<string> | undefined'.
  Overload 2 of 4, '(filter?: FilterQuery<GuildConfig> | undefined, projection?: ProjectionType<GuildConfig> | null | undefined): Query<...>', gave the following error.
    Type 'string | null' is not assignable to type 'Condition<string> | undefined'.
  Overload 3 of 4, '(filter?: FilterQuery<GuildConfig> | undefined): Query<(Document<unknown, {}, GuildConfig> & GuildConfig & { _id: ObjectId; }) | null, Document<...> & ... 1 more ... & { ...; }, {}, GuildConfig, "findOne">', gave the following error.
    Type 'string | null' is not assignable to type 'Condition<string> | undefined'.
Error: src/listeners/message/messageDelete.ts(25,[11](https://github.com/Anonymous4078/suggester/actions/runs/8382964808/job/22957771088?pr=327#step:6:12)): error TS2322: Type 'string | null' is not assignable to type 'Condition<string> | undefined'.
Error: Process completed with exit code 1.

My code used to work till v8.2.2 and types suddenly broke with 8.2.3

Steps to Reproduce

await collection.findOneAndUpdate(
            { guildId },
            {
              $pull: { suggestions: { messageId: message.id } },
            },
          );

Expected Behavior

The types shouldn't break in a semver patch change

Anonymous4078 commented 8 months ago

Needs a fix

vkarpov15 commented 8 months ago

@Anonymous4078 what does your schema look like?

Anonymous4078 commented 8 months ago

@vkarpov15 Simple schema which break is :

import { Schema, model } from 'mongoose';

interface GuildConfig {
  guildId: string;
  channelIds: string[];
}

const schema = new Schema<GuildConfig>({
  guildId: String,
  channelIds: [String],
});

export const collection = model<GuildConfig>('cooldowns', schema);
Anonymous4078 commented 8 months ago

Worked perfectly fine till version 8.2.2 and breaks in v8.2.3

vkarpov15 commented 7 months ago

I'm unable to repro, the following script compiles fine:

import { Schema, model } from 'mongoose';

interface GuildConfig {
  guildId: string;
  channelIds: string[];
}

const schema = new Schema<GuildConfig>({
  guildId: String,
  channelIds: [String],
});

const collection = model<GuildConfig>('cooldowns', schema);

const guildId = '42';
collection.findOneAndUpdate(
            { guildId },
            {
              $pull: { channelIds: 'test' },
            },
          );

output:

$ ./node_modules/.bin/tsc --strict gh-14462.ts 
$ 
$ head ./node_modules/typescript/package.json 
{
    "name": "typescript",
    "author": "Microsoft Corp.",
    "homepage": "https://www.typescriptlang.org/",
    "version": "5.4.3",
    "license": "Apache-2.0",
    "description": "TypeScript is a language for application scale JavaScript development",
    "keywords": [
        "TypeScript",
        "Microsoft",
$ head ./node_modules/mongoose/package.json 
{
  "name": "mongoose",
  "description": "Mongoose MongoDB ODM",
  "version": "8.3.0",
  "author": "Guillermo Rauch <guillermo@learnboost.com>",
  "keywords": [
    "mongodb",
    "document",
    "model",
    "schema",
$ 
github-actions[bot] commented 7 months ago

This issue is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 5 days

github-actions[bot] commented 7 months ago

This issue was closed because it has been inactive for 19 days and has been marked as stale.

Anonymous4078 commented 7 months ago

Not fixed

orgads commented 7 months ago

For my case it is resolved.

KeithGillette commented 7 months ago

mongoose@8.3.2 resolved the generics typing issues that I was seeing starting with 8.2.3, but 8.3.3 has reintroduced similar TS2322 errors in our TypeGoose project.

vkarpov15 commented 7 months ago

@KeithGillette please open a new issue and follow the issue template