-
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
Explore adding the Boolean to filter to narrow out null/undefined in array#filter #50377
base: main
Are you sure you want to change the base?
Conversation
It looks like you've sent a pull request to update our 'lib' files. These files aren't meant to be edited by hand, as they consist of last-known good states of the compiler and are generated from 'src/lib' or possibly our lib generator. Unless this is necessary, consider closing the pull request and sending a separate PR to update 'src/lib' or https://github.com/microsoft/TypeScript-DOM-lib-generator |
This PR doesn't have any linked issues. Please open an issue that references this PR. From there we can discuss and prioritise. |
>null : null | ||
|
||
const result = mixed.filter(Boolean) | ||
>result : (string | null | undefined)[] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moving to draft as this doesn't seem to actually work (and its late for me)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On my latest versions the readonly arrays work, but mutable arrays still aren't using the overload
93035d4
to
d283275
Compare
* @param predicate the `Boolean` constructor, which validates the truthiness of the value being mapped over. | ||
* @param thisArg An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value. | ||
*/ | ||
filter<S extends T>(predicate: BooleanConstructor, thisArg?: any): NonNullable<S>[]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like a hack solution. Is it to improve how ts infer the return type of a function so it can infer a type predicate?
arr.filter(x => typeof x === 'number')
// T[].filter<(x: T) => x is number>
arr.filter(x => x instanceof Q)
// T[].filter<(x: T) => x is Q>
arr.filter(x => x.kind === 1)
// ({ kind: 1, data: T } | { kind: 2, data: Q })[].filter<(x: ...) => x is { kind: 1, data: T }>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, this has nothing to do with inferring type predicates from function expressions, it’s just so you can use .filter(Boolean)
. See #50387.
Here's what I don't like about this const arr = [0, 1, "", "foo", null] as const;
const arr2 = arr.filter(Boolean);
// Error, good
if (arr2[0] === 3) {
}
// No error, bad
if (arr2[0] === 0) {
} The PR is incomplete (in the technical sense of the word) the same way the current behavior is -- elements which are removed from the array are still represented as being possibly-there by the type system. A non-coercing standalone function works well to eliminate surprising coercions declare function isNotNull<T>(obj: T): obj is NonNullable<T>;
const arr = [0, 1, "", "foo", null] as const;
const arr2 = arr.filter(isNotNull); |
I agree - I gave this another shot but couldn't get better results - do you think this could work as a CFA node? Otherwise I'm happy to close this PR |
From this tweet.
The main issue this attempts to solves is the design to drop null/undefined from an array in an ergonomic way by providing an override for
arr.filter(Boolean)
.