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

Explore adding the Boolean to filter to narrow out null/undefined in array#filter #50377

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

orta
Copy link
Contributor

@orta orta commented Aug 19, 2022

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).

@typescript-bot typescript-bot added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label Aug 19, 2022
@typescript-bot
Copy link
Collaborator

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

@typescript-bot
Copy link
Collaborator

This PR doesn't have any linked issues. Please open an issue that references this PR. From there we can discuss and prioritise.

@typescript-bot typescript-bot added the lib update PR modifies files in the `lib` folder label Aug 19, 2022
>null : null

const result = mixed.filter(Boolean)
>result : (string | null | undefined)[]
Copy link
Contributor Author

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)

Copy link
Contributor Author

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

@orta orta marked this pull request as draft August 19, 2022 21:28
* @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>[];
Copy link
Contributor

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 }>

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.

@RyanCavanaugh
Copy link
Member

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);

@orta
Copy link
Contributor Author

orta commented Aug 27, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Uncommitted Bug PR for untriaged, rejected, closed or missing bug lib update PR modifies files in the `lib` folder
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants