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

Array.filter callback parameter should not be required to return a boolean #7850

Closed
mjohnsonengr opened this issue Apr 6, 2016 · 1 comment
Labels
Duplicate An existing issue was already created

Comments

@mjohnsonengr
Copy link

The TypeScript definition for Array.filter specifies the callback must return a boolean, thus the code example at the bottom of this post fails to compile. Here's the current definition:

filter(callbackfn: (value: T, index: number, array: T[]) => boolean, thisArg?: any): T[];

The spec says this about the callback parameter:

callbackfn should be a function that accepts three arguments and returns a value that is coercible to the Boolean value true or false.

Thus I propose the definition should be changed to:

filter(callbackfn: (value: T, index: number, array: T[]) => any, thisArg?: any): T[];

Note: using a double not-operator is a sufficient workaround with negligible implications

TypeScript Version:
1.8.7

Code
Playground

// the purpose is to weed out falsey values
let a = [1, 2, 3, undefined, 0, 42, ""].filter(i => i)
// "i => i" is an error because it does not return boolean
// the following has no error
let b = [1, 2, 3, undefined, 0, 42, ""].filter(i => !!i)

Expected behavior:
No compile errors

Actual behavior:
Compile errors

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Apr 6, 2016
@RyanCavanaugh
Copy link
Member

This appears to be a duplicate of issue #5850

To reduce noise in issue searches, we close issues that are already tracked somewhere else. If you think the linked issue is not actually the same, please leave a comment here (we do read comments on closed issues). Otherwise you can follow or chime in on the linked issue. Thanks!

@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
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants