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 types for results and collections #14340
Conversation
now we can simply do something like: ``` const model = DB.objects<IUserContext>("UserContext").sorted("token", true); ``` And expect the model to be of the union of types`IUserContext & Realm.Object`
@@ -107,24 +107,24 @@ declare namespace Realm { | |||
* @param {any[]} ...arg | |||
* @returns Results | |||
*/ | |||
filtered(query: string, ...arg: any[]): Results; | |||
filtered(query: string, ...arg: any[]): Results<T & Object>; |
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.
T & Object
doesn't actually do anything. The apparent type of T
is {}
which is effectively the same as Object
- in short, you get all the same members of Object
anyway. Furthermore, number
and string
are assignable to Object
and {}
, so it doesn't do a ton for you.
I think what you really want to do is constrain T
to be of only an object type, which TypeScript 2.2 will have. See here
Until then, I think you'll just have to roll with 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.
Ignore the above - I just realized you were referring to Realm.Object
. Can you please make that explicit?
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 for the review. Done, updated to make explicit.
Can you update the version number at the top of the file? Keep it as the MAJOR.MINOR version (and remove anything else following that) Can you also add a { "extends": "../tslint.json" } @Akim95 can you review this? |
@DanielRosenwasser Thanks for the review, done. |
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.
Please update version to realm-js 0.14
@@ -1,4 +1,4 @@ | |||
// Type definitions for realm-js 0.14.3 | |||
// Type definitions for realm-js 1.0 |
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.
Please update version to realm-js 0.14
*/ | ||
slice(start?: number, end?: number): Object[]; | ||
slice(start?: number, end?: number): Array<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.
The linter is complaining about this being Array<T>
instead of T[]
.
|
||
/** | ||
* @param {(object:any,index?:any,collection?:any)=>void} callback | ||
* @param {any} thisArg? | ||
* @returns Object|void | ||
*/ | ||
find(callback: (object: any, index?: any, collection?: any) => void, thisArg?: any): Object | void; | ||
find(callback: (object: any, index?: any, collection?: any) => void, thisArg?: any): (T) | void; |
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.
Just T | null | undefined
/** | ||
* @returns Object|void | ||
*/ | ||
pop(): Object | void; | ||
pop(): (T) | void; |
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.
T | null | undefined
|
||
/** | ||
* @returns Object|void | ||
*/ | ||
shift(): Object | void; | ||
shift(): (T) | void; |
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.
T | null | undefined
@@ -152,14 +152,14 @@ declare namespace Realm { | |||
* @param {number} end? | |||
* @returns T[] | Object[] | |||
*/ | |||
slice(start?: number, end?: number): (T & Object)[]; | |||
slice(start?: number, end?: number): Array<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.
Could you update to 'T[]'. and at line 252.
@DanielRosenwasser @Akim95 Thanks for the review, updated. |
hey @DanielRosenwasser @Akim95 sorry to nag, but is there any chance of this PR getting merge soon? please let me know if you need any further change. thanks! |
Tested and anything works fine for me. You should wait maintainer to merge this PR or otherwise. |
Now if you declare a variable like:
You can expect that ob's type is
IUserContext
Also works for creation of objects: