[TypeScript] watch().on doesn't have correct type on callback, so can't access fullDocument #13206
Closed
2 tasks done
Labels
typescript
Types or Types-test related issue / Pull Request
Milestone
Prerequisites
Mongoose version
7.0.2 (but happening since 6.4.0, with the switch to mongodb driver 4.7.0)
Node.js version
19.8.1
MongoDB server version
6.0.4
Typescript version (if applicable)
5.0.2
Description
watch().on
is returning the genericChangeStreamDocument<T>
as argument for theon
callback, which doesn't includefullDocument
and other properties from other types of specializedChangeStreamDocument
types likeChangeStreamInsertDocument
orChangeStreamUpdateDocument
that were introduced in mongodb driver 4.6.0.I suppose it's because the current type for
watch()
accepts only 1 argument, but since mongodb 4.6.0, watch type requires two arguments to be able to provide the specialized return types:https://github.com/mongodb/node-mongodb-native/releases/tag/v4.6.0
See current docs https://mongodb.github.io/node-mongodb-native/5.1/classes/Collection.html#watch
Steps to Reproduce
Will give a TypeScript error:
Property 'fullDocument' does not exist on type 'ChangeStreamDocument<Schema>'.
Expected Behavior
Being able to call
watch
with specialized type, like:So the
change
argument of theon
callback has the correct type, to for example be able to accessfullDocument
.Workaround
As a workaround, I'm setting the type of the
on
parameter explicitly to the specialized types I need:But I think it makes more sense to align the current mongoose's
watch
to how it is in the current mongodb driver.The text was updated successfully, but these errors were encountered: