5

It seems like some package upgrade with mongoose or @types/mongoose is now causing new typescript errors from mongoose $push, $pull, $addToSet, and $each operators. For example:

await User.findByIdAndUpdate(request.user._id, {
      $push: {
        mediaList: { $each: mediaIDs },
      },
    });

Hovering over $each I can see:

   Type 'string[]' is not assignable to type 'never'.ts(2322)

I'm getting this same problem with the other operators as well (type "whatever given type" is not assignable to type never). I suppose it could also have to do with webpack upgrading and ts-loader upgrades. I'm using:

"@types/mongoose": "^5.2.4",
"mongoose": "^5.2.4",
"ts-jest": "^21.2.4",
"ts-loader": "4.1.0",
"ts-node": "^3.2.0",
"tslint": "^5.11.0",
"typescript": "^3.1.6",
"webpack": "^4.42.1",
"webpack-cli": "^3.3.11"
Ariel Frischer
  • 1,406
  • 2
  • 12
  • 20
  • Same issue with me. https://prnt.sc/txvg8l Have you found solution? – LBeerus Aug 11 '20 at 16:09
  • Adding //@ts-ignore to the line above worked for me. It's not real solution I guess but yea couldn't really solve this. – Ariel Frischer Aug 11 '20 at 20:35
  • 1
    Yeah that helped, but we are losing TS type checking that way. Also I've noticed that this issue only occurs with $inc operator, so if I change above query to $set it works smoothly. Its definitely something related to @types/mongoose, hope they will fix it in next version. Thanks for help once more I will use that, because query with $inc is much more cleaner. – LBeerus Aug 14 '20 at 06:44

3 Answers3

1

Adding //@ts-ignore to the line above seems to calm down the typescript checker and the query still works.

Ariel Frischer
  • 1,406
  • 2
  • 12
  • 20
1

I'm not sure if this is the same issue as yours, but I was facing the same issue today, The error was in my definition of the field. I had following in my mongoose document.

attachments?: AttachmentDocument["_id"]

where it should be:

attachments?: AttachmentDocument["_id"][]

As it is an array of documents the above change fixed my issue.

Adding it as answer to help out someone, not sure if it is actual answer to the question.

Farhan Tahir
  • 2,096
  • 1
  • 14
  • 27
1

Maybe check your node version.

I had this issue because my current local node version wasn't the version set in the package.json

Check if you have a node version specified in your package.json

"engines": {
    "node": "^15.7.0"
}

Remove node_modules and package-lock.json

rm -Rf node_modules package-lock.json

Set your correct node version

echo "v15.7.0" > .nvmrc
nvm install
nvm use

And finally reinstall packages

npm install
ade-verd
  • 71
  • 1
  • 2