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 types don't support dot syntax for $pull #9993

Closed
fschucht opened this issue Mar 3, 2021 · 3 comments
Closed

Typescript types don't support dot syntax for $pull #9993

fschucht opened this issue Mar 3, 2021 · 3 comments
Assignees
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@fschucht
Copy link

fschucht commented Mar 3, 2021

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

What is the current behavior?
Using findOneAndUpdate, findByIdAndUpdate, updateOne or updateMany with the $pull operator and dot syntax causes a Typescript compilation error. The syntax itself works both with mongoose (if the typescript error is silenced) and within the mongo console. Using a nested object does not work in this case as it's not supported my mongo.

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

import { ObjectId } from 'mongodb';
import mongoose, { Document, Schema } from 'mongoose';

interface ActorInterface {
  type: string;
  id: ObjectId;
}

interface TagInterface {
  value: string;
  actor: ActorInterface;
}

interface PostInterface extends Document {
  tags: TagInterface[]
}

const postSchema = new Schema({
  tags: [{
    value: String,
    actor: {
      type: String,
      id: Schema.Types.ObjectId
    }
  }]
})

const postModel = mongoose.model<PostInterface>('Post', postSchema)

function removeTagsByActor(postId: ObjectId, actorId: ObjectId) {
  return postModel.findOneAndUpdate({
    _id: postId
  }, {
    $pull: {
      tags: {
        'actor.id': actorId // <-- compilation error
      }
    }
  })
}

Resulting error:

Type '{ 'actor.id': any; }' is not assignable to type 'Partial<Pick<Pick<_LeanDocument<TagInterface>, "value" | "actor">, "value" | "actor">> | { value?: QuerySelector<string> | undefined; actor?: QuerySelector<...> | undefined; } | undefined'.
  Object literal may only specify known properties, and ''actor.id'' does not exist in type 'Partial<Pick<Pick<_LeanDocument<TagInterface>, "value" | "actor">, "value" | "actor">> | { value?: QuerySelector<string> | undefined; actor?: QuerySelector<...> | undefined; }'.ts(2322)

tsconfig.json:

{
  "compilerOptions": {
    "module": "commonjs",
    "resolveJsonModule": true,
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "noImplicitAny": false,
    "noImplicitThis": false,
    "strictNullChecks": true,
    "allowSyntheticDefaultImports": true,
    "strictPropertyInitialization": false,
    "strictBindCallApply": true,
    "strictFunctionTypes": false,
    "target": "es2017",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true,
    "typeRoots": [
      "./typings",
      "./node_modules/@types"
    ],
    "esModuleInterop": true,
  },
  "exclude": [
    "node_modules",
    "dist",
    "./typings"
  ]
}

What is the expected behavior?
There should be no compilation error and the dot syntax should be allowed.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.

  • Mongoose: 8.11.18
  • Node.js: 12.8.0
  • Typescript: 4.1.3
@IslandRhythms IslandRhythms added the typescript Types or Types-test related issue / Pull Request label Mar 4, 2021
@vkarpov15 vkarpov15 added this to the 5.11.20 milestone Mar 8, 2021
@vkarpov15 vkarpov15 self-assigned this Mar 10, 2021
This was referenced Mar 11, 2021
This was referenced Mar 13, 2021
@AntoRin
Copy link

AntoRin commented Jun 28, 2021

Were you able to find a solution? I just ran into this error, and have no idea why this happens.

@fschucht
Copy link
Author

@AntoRin We updated to version 5.12.0 which solved the issue for us.

@AntoRin
Copy link

AntoRin commented Jun 29, 2021

@AntoRin We updated to version 5.12.0 which solved the issue for us.

Thanks for responding, @fschucht . I myself am using 5.12.2, but the problem persists. The transpiled build works without any problem, so I guess I should just ignore the error.

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