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

Inferred Type Predicates don't work with the Boolean function #58999

Closed
jakeleventhal opened this issue Jun 24, 2024 · 3 comments
Closed

Inferred Type Predicates don't work with the Boolean function #58999

jakeleventhal opened this issue Jun 24, 2024 · 3 comments

Comments

@jakeleventhal
Copy link

πŸ”Ž Search Terms

Inferred Type Predicates Boolean

πŸ•— Version & Regression Information

This is a new issue introduced in 5.5

⏯ Playground Link

https://www.typescriptlang.org/play/?isolatedDeclarations=true#code/PTAEGEHsCdoUwMYBcA2BPUBLAdgMztAM6hJoAOcoAhsYUtDgOYBQCk2doA7jANaEAuUHQbZGAbQC6oALyhxAOiUBBWFTQAKAKwBKSc1CHQCgLZUyGgG6yAfKADeBo88y5QV0AFIATLJlyABh0HJ2cw0HgkAFdobFAAcipheiZ4gG5QsIBfZkznSJi47CiUFAznLJ1MhVxMFCQCDxk7AEIWyx0M5hBQABFIOEJseKRuPlBcGGFIE0p4GnZQOEs4OKQAC0goxnWsJGIAIxpMBCpSjA3KQipZ1nZOABMBoaQAdT4ARiERJilZeSUClU0HU2j01TMFmszRC4UMrnc1h8fkCwUccLCBViCSSPzE6TyRhyhMMWKKJTKmUq1Vq9Ua0LsHS6PXAVGwoxw+GgoAAQpBICg4GyxtBeEw7hxRk9Buz3qLvN8UmI-nJFCo1JpdPpnKZzE07Oi4QiPMj-KjYRijGScclRIwCRjiRjrcVSuUiVUdbSGtANHyBULsJ0JY9nrK+ABmRV2lUA9UgzXgnWQ-UW8LGpG+M2gIJpy3WxK21Lu8JOuEuikl0DUr11H2p-2CtlWHTB5hAA

πŸ’» Code

// Correctly infers type as string
const works: string[] = [...Array(5)]
    .map(v => {
        if (v %2 === 0) {
            return 'a string';
        }

        return null;
    })
    .filter(v => !!v);

// Doesn't work for some reason even though its basically the same
const doesntWork1: string[] = [...Array(5)]
    .map(v => {
        if (v %2 === 0) {
            return 'a string';
        }

        return null;
    })
    .filter(v => v);

// Cant infer Boolean working
const doesntWork2: string[] = [...Array(5)]
    .map(v => {
        if (v %2 === 0) {
            return 'a string';
        }

        return null;
    })
    .filter(Boolean);
const doesntWork3: string[] = [...Array(5)]
    .map(v => {
        if (v %2 === 0) {
            return 'a string';
        }

        return null;
    })
    .filter(v => Boolean(v));

πŸ™ Actual behavior

See playground; it can't infer types properly.

πŸ™‚ Expected behavior

All have the same type

Additional information about the issue

No response

@jakebailey
Copy link
Member

Boolean is not a type predicate. See #50387 (comment) and #16655

@MichalMarsalek
Copy link

#57465 (comment)

@jakeleventhal
Copy link
Author

Closing, but this is kind of sad

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

No branches or pull requests

3 participants