You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Under the current type definitions, collections and cursors return documents that are always typed as Object. This means that when using TypeScript, documents must always be type-asserted to a concrete type before being used:
constdb=newzango.Db('mydb');constpeople=db.collection('people');constperson=people.find({name: 'John'});person.name='Mike';// ERROR: Property 'ok' does not exist on type 'Object'.interfacePerson{name: string;}constperson2=people.find({name: 'John'})asPerson;person.name='Mike';// OK
This happens because current type definitions are hard-typed to Object and do not allow generics:
We could get inspired by mongodb's type definitions at DefinitelyTyped, and generalize Cursor and Collection, allowing the library consumer to set the document's interface for a given collection, falling back to Record<string, any> if the type parameter is not supplied:
Using a similar sytem, we could accomplish the following with TypeScript:
interfacePerson{name: string;}constdb=newzango.Db('mydb');constpeople=db.collection<Person>('people');constperson=people.find({name: 'John'});// typeof person == Personperson.name='Mike';// OK
I can go ahead and write a quick PR to fix this if you'd like.
The text was updated successfully, but these errors were encountered:
Under the current type definitions, collections and cursors return documents that are always typed as
Object
. This means that when using TypeScript, documents must always be type-asserted to a concrete type before being used:This happens because current type definitions are hard-typed to
Object
and do not allow generics:zangodb/src/zangodb.d.ts
Lines 21 to 31 in 868babf
We could get inspired by mongodb's type definitions at DefinitelyTyped, and generalize
Cursor
andCollection
, allowing the library consumer to set the document's interface for a given collection, falling back toRecord<string, any>
if the type parameter is not supplied:mongodb's
Collection
type signature - allows a type parametermongodb's
Collection#findOne
type signature - leverages collection type parameterUsing a similar sytem, we could accomplish the following with TypeScript:
I can go ahead and write a quick PR to fix this if you'd like.
The text was updated successfully, but these errors were encountered: