Skip to content

support type predicate callback functions on Array<T>.filter #11858

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

Closed
wants to merge 1 commit into from
Closed

support type predicate callback functions on Array<T>.filter #11858

wants to merge 1 commit into from

Conversation

sebek64
Copy link

@sebek64 sebek64 commented Oct 26, 2016

This overload of Array.filter function seems useful, but I'm not sure if it doesn't break something.

@msftclas
Copy link

Hi @sebek64, I'm your friendly neighborhood Microsoft Pull Request Bot (You can call me MSBOT). Thanks for your contribution!

In order for us to evaluate and accept your PR, we ask that you sign a contribution license agreement. It's all electronic and will take just minutes. I promise there's no faxing. https://cla.microsoft.com.

TTYL, MSBOT;

@msftclas
Copy link

@sebek64, Thanks for signing the contribution license agreement so quickly! Actual humans will now validate the agreement and then evaluate the PR.

Thanks, MSBOT;

@RyanCavanaugh
Copy link
Member

Looks correct to me

@HerringtonDarkholme
Copy link
Contributor

Shouldn't this go to src/lib ?

@sebek64
Copy link
Author

sebek64 commented Oct 27, 2016

Yes, I haven't read the contributing instructions with the required attention. I'll provide a fix.

@sebek64
Copy link
Author

sebek64 commented Oct 27, 2016

I've pushed an updated version. I'm not an expert on typescript internals. I've 'baseline-accept'ed the changes, but I don't know if that is correct.

I've found that ReadonlyArray already has these overloads both in lib/ and src/lib. So this commit adds a similar overloads to Array.

lib/lib.d.ts Outdated
@@ -1258,6 +1258,12 @@ interface Array<T> {
* @param callbackfn A function that accepts up to three arguments. The filter method calls the callbackfn function one time for each element in the array.
* @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
*/
filter<S extends T>(callbackfn: (value: T, index: number, array: T[]) => value is S, thisArg?: any): S[];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The files under lib are not edited manually, they are generated as part of the build. please remove them. you only need to update src\lib

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, removed all changes of lib/* files.

lib/lib.es5.d.ts Outdated
@@ -1258,6 +1258,12 @@ interface Array<T> {
* @param callbackfn A function that accepts up to three arguments. The filter method calls the callbackfn function one time for each element in the array.
* @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
*/
filter<S extends T>(callbackfn: (value: T, index: number, array: T[]) => value is S, thisArg?: any): S[];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change the other filter methods on ReadOnlyArray and the TypedArray variants e.g. Int32Array.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As for ReadonyArray, this method is already there. For typed arrays, it doesn't make sense to me (since there is no type inheritance like for generic arrays).

@maiermic
Copy link

This pull request seem to be a duplicate of #10916

@sebek64
Copy link
Author

sebek64 commented Jan 24, 2017

Yes, that's true. I haven't noticed it when I created this one. So please close one of them and merge the other one.

@maiermic
Copy link

@sebek64 I don't have the permission to close this pull request and I'm waiting for @mhegazy to merge #10916

 - this is already supported on ReadonlyArray<T>
@sebek64
Copy link
Author

sebek64 commented Apr 24, 2017

I've rebased the branch to current master. Is there any possibility that this gets merged before it runs out of sync again?

* @param callbackfn A function that accepts up to three arguments. The filter method calls the callbackfn function one time for each element in the array.
* @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
*/
filter<S extends T>(callbackfn: (this: void, value: T, index: number, array: T[]) => value is S): S[];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ReadonlyArray and Array should stay in sync. so please update both.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also consider coalescing the overloads using type argument defaults:

filter<S extends T, Z = undefined>(callbackfn: (this: Z, value: T, index: number, array: T[]) => value is S, thisArg?: Z): S[];

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ad first: ReadonlyArray is already updated (see commit comment), so this commit actually brings both classes in sync.

Ad second: I'm not sure if this is the same, as Z is void in the first case and undefined in second. And tt is used the same way 5 lines below the change.

@maiermic
Copy link

This issue has been fixed by #16223 and can be closed. You can try it out in [email protected] for example.

@sebek64 sebek64 closed this Jul 17, 2017
@sebek64 sebek64 deleted the support-type-predicates-on-array-filter branch July 17, 2017 18:39
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants