Skip to content
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

Merged
merged 2 commits into from Feb 4, 2017
Merged

Add generic types for results and collections #14340

merged 2 commits into from Feb 4, 2017

Conversation

rodrigopivi
Copy link
Contributor

@rodrigopivi rodrigopivi commented Jan 30, 2017

Now if you declare a variable like:

const ob = DB.objects<IUserContext>("UserContext").sorted("token", true);

You can expect that ob's type is IUserContext

Also works for creation of objects:

DB.create(DB.objects<IRole>("Role"), { xx: "type checked field" });

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>;
Copy link
Member

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.

Copy link
Member

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?

Copy link
Contributor Author

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.

@DanielRosenwasser DanielRosenwasser dismissed their stale review January 30, 2017 17:59

Misunderstood change.

@DanielRosenwasser
Copy link
Member

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 tslint.json file that contains the following line?

{ "extends": "../tslint.json" }

@Akim95 can you review this?

@rodrigopivi
Copy link
Contributor Author

@DanielRosenwasser Thanks for the review, done.

Copy link
Contributor

@Akim95 Akim95 left a 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
Copy link
Contributor

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>;
Copy link
Member

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;
Copy link
Member

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;
Copy link
Member

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;
Copy link
Member

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>;
Copy link
Contributor

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.

@rodrigopivi
Copy link
Contributor Author

@DanielRosenwasser @Akim95 Thanks for the review, updated.

@rodrigopivi
Copy link
Contributor Author

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!

@Akim95
Copy link
Contributor

Akim95 commented Feb 1, 2017

Tested and anything works fine for me. You should wait maintainer to merge this PR or otherwise.

@bowdenk7 bowdenk7 merged commit 873860b into DefinitelyTyped:master Feb 4, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants