Closed replete closed 3 years ago
It looks like DocumentDefinition
was removed in Mongoose 6, reverting to 5.13.11
resolves the issue, but introduces various other required code warnings such as
(node:97087) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect. (node:97087) [MONGODB DRIVER] Warning: Current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
My temporary fix attempt, example user.service.ts
-import { DocumentDefinition, FilterQuery } from 'mongoose'
+import { FilterQuery } from 'mongoose'
...
+// Temp fix for DocumentDefinition in 6x
+import { _AllowStringsForIds, LeanDocument } from 'mongoose'
+type DocumentDefinition<T> = _AllowStringsForIds<LeanDocument<T>>
I took this from version 5.13.11
. DocumentDefinition
was exported in index.d.ts
but only used in test/typescript/schema.ts
which might explain why it was removed, and isn't in any official documentation 🤷♂️
...fails in Jest though:
src/controller/product.controller.ts:18:38 - error TS2345: Argument of type '{ user: any; title: string; description: string; price: number; image: string; }' is not assignable to parameter of type 'DocumentDefinition<Omit<ProductDocument, "createdAt" | "updatedAt">>'.
Types of property 'price' are incompatible.
Type 'number' is not assignable to type 'string | LeanDocument<any>'.
18 const product = await createProduct({ ...body, user: userId })
~~~~~~~~~~~~~~~~~~~~~~~~~
I'm a little out of my depth here being new to TypeScript, Express and Mongoose 😅
The types used for the services are just mirroring what Mongoose expects. So if you command-click into the mongoose method, you can explore the interface and see what types it expects.
Thanks for your response, and sharing this tutorial with us.
Any ideas why it doesn't like price: number
?
How would you go about fixing this, make another interface?
Are you still working on a Jest integration tutorial? If so I guess you'll face this problem.
I created a video about how to fix this issue: https://youtu.be/5-1KuU-21uI
The testing video has also been out for a few days now: https://youtu.be/r5L1XRZaCR0
Thanks so much for doing that, Tom! Legendary.
First of all, thanks for this tutorial, I've been using it to learn TypeScript and get back to development after a few years on development hiatus.
I've been working on this codebase, and today just started having a go at implementing Jest, during which I've encountered a few head-scratchers which probably weren't a problem before because the project was using
@types/mongoose
definitions, which were for version 5.11.97, mismatching version 6 of themongoose
package used in this project.(
@types/mongoose
is now deprecated so this problem will turn up when you remove the package and rely onmongoose
package's built-in definitions)My first attempt to resolve was replacing
DocumentDefinition
withDocument
, which creates this error:Any ideas?
P.S. How's the Jest integration coming on? I was trying to implement Jest with supertest