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

Is $in / $nin broken for enums in TypeScript? #10757

Closed
cloakedch opened this issue Sep 21, 2021 · 2 comments
Closed

Is $in / $nin broken for enums in TypeScript? #10757

cloakedch opened this issue Sep 21, 2021 · 2 comments
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@cloakedch
Copy link

cloakedch commented Sep 21, 2021

Do you want to request a feature or report a bug?
Bug

What is the current behavior?
$in and $nin types used to support enums in mongoose 5.x, now with 6.x, there is a type error with the latest NestJS.

If the current behavior is a bug, please provide the steps to reproduce.

export enum MyEnum {
  VALUE1,
  VALUE2,
  VALUE3
}

export type MyClassDocument = MyClass & Document;

@Schema()
export class MyClass {
  @Prop({ default: MyEnum.VALUE1 })
  status: MyEnum;
}

const test: FilterQuery<MyClassDocument> = { status: { $in: [MyEnum.VALUE1, MyEnum.VALUE2] } };

Now $in is marked as having an error, since

Type '(MyEnum.VALUE1 | MyEnum.VALUE2)[]' is not assignable to type 'MyEnum.VALUE1[] | MyEnum.VALUE2[] | MyEnum.VALUE3[]'

This used to work fine in mongoose 5.x, and I could not find any hints about this change in the changelog.

The last line above works fine (in regards to tsc) when using

const test: FilterQuery<MyClassDocument> = { status: { $in: [0, 1] } };

Obviously, that is not a solution.

tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "target": "es2017",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true
  }
}

What is the expected behavior?
Supported behaviour, no error.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Node v14.17.6
NestJS 8.1.1
MongoDB 5 (though irrelvant here)
Mongoose 6.0.7

@cloakedch cloakedch changed the title Is $in / $nin broken for enums in TypeScript Is $in / $nin broken for enums in TypeScript? Sep 21, 2021
@cloakedch
Copy link
Author

Possibly related: #10734

@IslandRhythms IslandRhythms added the typescript Types or Types-test related issue / Pull Request label Sep 22, 2021
@IslandRhythms
Copy link
Collaborator

import * as mongoose from 'mongoose';

enum MyEnum {
    VALUE1,
    VALUE2,
    VALUE3
}


class MyClass {
    test: ({default: MyEnum.VALUE1})
    status: MyEnum
}

type MyClassDocument = MyClass & mongoose.Document;

const test: mongoose.FilterQuery<MyClassDocument> = {status: { $in: [MyEnum.VALUE1, MyEnum.VALUE2]}}

@vkarpov15 vkarpov15 added this to the 6.0.11 milestone Sep 24, 2021
@vkarpov15 vkarpov15 modified the milestones: 6.0.11, 6.0.10 Oct 5, 2021
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