Skip to content

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

Closed
@jakeleventhal

Description

@jakeleventhal

πŸ”Ž 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions