-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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.prototype.filter definition forces a boolean #5850
Comments
how about: a.filter(x => !! x.prop) Alternatively, you can define a new signature for filter in a file in your project as such: interface Array<T> {
filter(callbackfn: (value: T, index: number, array: T[]) => any, thisArg?: any): T[];
} |
Well, yes, type casting is the current way to appease the compiler.
Sure, thanks for the suggestion. I know how to do that. My point was not how to workaround this limitation in my project, I have no problem with that. |
We've run into this sort of problem with If we did ever want to fix this, we couldn't just change the return type to |
@DanielRosenwasser, you’re right, this is a judgement call about inconvenience vs catching errors. Do consider, that in my point of view, |
I'm with @denis-sokolov on this one. We shouldn't pretend |
+1, it is inconsistent to have different levels of strictness for |
Approved. @mhegazy said there's no other suspicious use of Return type of the callback should be |
Why should the return type be |
I was arguing for |
Oh, and function fn() { }
if (fn()) { // <-- not an error
} |
Why would you ever do that instead of using |
I use typescript because I like static typing, I'm not sure I like this. If this is ok, who'd say that no more ugly type coersions in javascript won't diffuse into typescript "because we can do it in javascript"? I'd counter-propose a breaking change for the if and require an explicit boolean there instead. In the end, typescript is all about adding the layer of typing that JS is missing right?. |
@Elephant-Vessel I'd agree with you were this some other kind of type coersion, but boolean coersion in the form of truthy and falsey values has become a staple of the language for many TypeScript developers, and doesn't detract from the purpose of the language at all. Your breaking change proposal would break such a large portion of TypeScript codebases out there. Would you also propose breaking changes for && and || so I can't do this:
|
A coherent conceptual model of a language is more important for me than some neat shorthands - So yes I do, that should at least return either true or defaultProp. And preferably complain about the implicit conversions to booleans. Of course I was rhetorical about actually making this a breaking change, but it could at least be a compiler setting for us that don't want these compromises. Edit: Oh and by the way, that stuff is in my opinion better handled by giving us a decent set of operators:
|
Fixed in #7779 |
It is natural to use
Array.prototype.filter
to filter items with missing information:Unfortunately, this does not compile, as
filter
definition requires the result of the filter function to be strictly boolean. Would it make sense to make thefilter
definition less strict?The text was updated successfully, but these errors were encountered: