-
Notifications
You must be signed in to change notification settings - Fork 29.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add generic support to Collection and it's methods #16315
Conversation
TypeScript support default generic since version 2.3, so add generic to Collection.
types/mongodb/index.d.ts to author (@CaselIT). Could you review this PR? Checklist
|
I checked logs, it says there are some syntax error. But there is no problem on my code, so strange... 😢 # test logs
../mongodb/index.d.ts(184,24): error TS1005: ',' expected.
../mongodb/index.d.ts(185,24): error TS1005: ',' expected.
../mongodb/index.d.ts(186,24): error TS1005: ',' expected.
../mongodb/index.d.ts(195,30): error TS1005: ',' expected.
../mongodb/index.d.ts(196,30): error TS1005: ',' expected.
../mongodb/index.d.ts(197,30): error TS1005: ',' expected.
../mongodb/index.d.ts(239,30): error TS1005: ',' expected.
../mongodb/index.d.ts(240,30): error TS1005: ',' expected.
../mongodb/index.d.ts(241,30): error TS1005: ',' expected.
../mongodb/index.d.ts(424,17): error TS1005: ',' expected.
../mongodb/index.d.ts(425,17): error TS1005: ',' expected.
../mongodb/index.d.ts(466,12): error TS1005: ',' expected.
../mongodb/index.d.ts(469,15): error TS1005: ',' expected.
../mongodb/index.d.ts(470,15): error TS1005: ',' expected. |
@beary the build fails because of microsoft/dtslint#31. Shortly DT will start supporting features that require the latest version of TS about a month after its release. Otherwise LGTM 👍 |
Thanks for your response @CaselIT |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs a // TypeScript Version: 2.3
comment on line 5.
types/mongodb/index.d.ts
Outdated
/** @deprecated */ | ||
find(query: Object, fields?: Object, skip?: number, limit?: number, timeout?: number): Cursor<any>; | ||
find<T = TSchema>(query: Object, fields?: Object, skip?: number, limit?: number, timeout?: number): Cursor<T>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is T
ever not TSchema
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, When fields
parameter was passed in, the results would not have some properties which TSchema
has. This is an old api http://mongodb.github.io/node-mongodb-native/1.4/api-generated/collection.html#find
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, my expression was puzzled. Actually I means T
always same as TSchema
, but if developer pass fields
parameter, they can specify T
according their fields
@beary if you could add the |
types/mongodb/index.d.ts
Outdated
|
||
// Documentation : http://mongodb.github.io/node-mongodb-native/2.2/api/ | ||
|
||
// TypeScript Version: 2.3 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Old location was correct.
Packages that depend on mongo will also have to be updated to have this in their header. #16198 is doing this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
@beary I had the same problem when have tried to migrate
|
@CaselIT Are you going to do it manually? If so It is pretty a lot of job there. I think it would be more better or quicker if somebody would write script that automatically resets all the headers in specified modules.
Yep, it is. |
@CaselIT At first I didn't understand you. See my post above again pls. 😄 |
@hinell Yes I did it manually. It's not too much work |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apart from the note I've left it looks good. 👍
types/mongodb/index.d.ts
Outdated
@@ -743,9 +741,9 @@ export interface DeleteWriteOpResultObject { | |||
} | |||
|
|||
//http://mongodb.github.io/node-mongodb-native/2.1/api/Collection.html#~findAndModifyWriteOpResult | |||
export interface FindAndModifyWriteOpResultObject { | |||
export interface FindAndModifyWriteOpResultObject<TSchema> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's better to set <TSchema=Default>
types/mongodb/index.d.ts
Outdated
|
||
// Documentation : http://mongodb.github.io/node-mongodb-native/2.2/api/ | ||
|
||
// TypeScript Version: 2.3 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
types/mongodb/index.d.ts
Outdated
@@ -443,7 +443,7 @@ export interface FSyncOptions { | |||
} | |||
|
|||
// Documentation : http://mongodb.github.io/node-mongodb-native/2.1/api/Collection.html | |||
export interface Collection { | |||
export interface Collection<TSchema> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Collection
was created by DB.collection()
and other methods of DB
, So I add default type argument to those methods but Collection
interface. But the test was failed, I'll and a Default
type argument to Collection
interface.
@CaselIT Thanks for remind, default type argument for |
@yuit Thank you! |
Please fill in this template.
npm run lint package-name
(ortsc
if notslint.json
is present).Select one of these and delete the others:
If changing an existing definition:
tslint.json
containing{ "extends": "dslint/dt.json" }
.TypeScript support default generic since version 2.3, So I add generic to collection. When create a collection by
DB.collection()
orDB.createCollection()
, developer could specify a scheme, and when do some query, the result can be static analysd.These changes don't break the old cold.
eg: