diff --git a/src/change_stream.ts b/src/change_stream.ts index 60b7cf7500..684eec7097 100644 --- a/src/change_stream.ts +++ b/src/change_stream.ts @@ -184,23 +184,23 @@ export interface UpdateDescription { } /** @public */ -export type ChangeStreamEvents = { +export type ChangeStreamEvents = { resumeTokenChanged(token: ResumeToken): void; - init(response: Document): void; - more(response?: Document | undefined): void; + init(response: TSchema): void; + more(response?: TSchema | undefined): void; response(): void; end(): void; error(error: Error): void; - change(change: ChangeStreamDocument): void; + change(change: ChangeStreamDocument): void; } & AbstractCursorEvents; /** * Creates a new Change Stream instance. Normally created using {@link Collection#watch|Collection.watch()}. * @public */ -export class ChangeStream< - TSchema extends Document = Document -> extends TypedEventEmitter { +export class ChangeStream extends TypedEventEmitter< + ChangeStreamEvents +> { pipeline: Document[]; options: ChangeStreamOptions; parent: MongoClient | Db | Collection; diff --git a/test/types/mongodb.test-d.ts b/test/types/mongodb.test-d.ts index 3ec4f79db5..204c5ca934 100644 --- a/test/types/mongodb.test-d.ts +++ b/test/types/mongodb.test-d.ts @@ -3,6 +3,7 @@ import { MongoClient } from '../../src/mongo_client'; import { Collection } from '../../src/collection'; import { AggregationCursor } from '../../src/cursor/aggregation_cursor'; import type { FindCursor } from '../../src/cursor/find_cursor'; +import type { ChangeStreamDocument } from '../../src/change_stream'; import type { Document } from 'bson'; import { Db } from '../../src'; import { Topology } from '../../src/sdam/topology'; @@ -19,9 +20,14 @@ expectDeprecated(Db.prototype.unref); expectDeprecated(MongoDBDriver.ObjectID); expectNotDeprecated(MongoDBDriver.ObjectId); +interface TSchema extends Document { + name: string; +} + // test mapped cursor types const client = new MongoClient(''); -const coll = client.db('test').collection('test'); +const db = client.db('test'); +const coll = db.collection('test'); const findCursor = coll.find(); expectType(await findCursor.next()); const mappedFind = findCursor.map(obj => Object.keys(obj).length); @@ -38,6 +44,17 @@ const composedMap = mappedAgg.map(x => x.toString()); expectType>(composedMap); expectType(await composedMap.next()); expectType(await composedMap.toArray()); +const tschemaColl = db.collection('test'); +const changeStream = tschemaColl.watch(); +changeStream.on('init', doc => { + expectType(doc); +}); +changeStream.on('more', doc => { + expectType(doc); +}); +changeStream.on('change', doc => { + expectType>(doc); +}); const builtCursor = coll.aggregate(); // should allow string values for the out helper