Skip to content

Array predicates should not have to return boolean #27509

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

Conversation

NikolajDL
Copy link

Changes predicate return type from boolean to any for array operations find, findIndex, some and every, such that the behavior matches that of the filter operation.

Fixes #27496

@msftclas
Copy link

msftclas commented Oct 2, 2018

CLA assistant check
All CLA requirements met.

@RyanCavanaugh
Copy link
Member

Test bench:

function filter1<T>(arr: T[], pred: (arg: T) => any) { }
function filter2<T>(arr: T[], pred: (arg: T) => unknown) { }
function filter3<T>(arr: T[], pred: (arg: T) => boolean) { }
function filter4<T>(arr: T[], pred: (arg: T) => {}) { }
function filter5<T>(arr: T[], pred: (arg: T) => {} | null | undefined) { }

const arr = [0];

filter1(arr, i => { });
filter1(arr, i => "");
filter1(arr, i => { return; });
filter1(arr, i => { return null; });
filter1(arr, i => { return undefined; });

filter2(arr, i => { });
filter2(arr, i => "");
filter2(arr, i => { return; });
filter2(arr, i => { return null; });
filter2(arr, i => { return undefined; });

filter3(arr, i => { });
filter3(arr, i => "");
filter3(arr, i => { return; });
filter3(arr, i => { return null; });
filter3(arr, i => { return undefined; });

filter4(arr, i => { });
filter4(arr, i => "");
filter4(arr, i => { return; });
filter4(arr, i => { return null; });
filter4(arr, i => { return undefined; });

filter5(arr, i => { });
filter5(arr, i => "");
filter5(arr, i => { return; });
filter5(arr, i => { return null; });
filter5(arr, i => { return undefined; });

I think filter5 has the best behavior of these when looking at both strictNullChecks on and off

@NikolajDL
Copy link
Author

I agree, but it would still be slightly more restrictive than the filter function in plain JavaScript. Following the discussion in #5850 it sounds like filter5 has the desired behavior.

Should I change the filter function in the same go or should it be a subject of a separate PR?

…r array functions find, findIndex, some and every

- Extends test cases for previous commits in this branch, to better reflect the expected behavior
@RyanCavanaugh
Copy link
Member

I'm going to take this to the design meeting

@RyanCavanaugh
Copy link
Member

This one's a bit out of date and we'd prefer a change to unknown per discussion notes. Sorry for the long timeline on this PR.

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.

Array functions taking predicates should not have to return boolean.
3 participants