Skip to content

Treat Boolean(x) equally as double not !!x #37180

Closed
@maxkomarychev

Description

@maxkomarychev

In complex boolean expressions it's clearer to do Boolean(x) cast rather than !!x in favor of readability. Also the latter way of casting something to boolean is error prone and it's easy to mistype !. Typescript should treat Boolean(x) the same way as !!.

TypeScript Version: 3.6.3,3.7.5,3.8.3 (tried top 3 available in playground)

Search Terms:

boolean cast non null
double negation boolean cast
double not Boolean

Code

interface A {
    a: number
}

const a: A | undefined

console.log(a && a?.a) // works
console.log(!!a && a.a) // works
console.log(Boolean(a) && a.a) // warns that `Object is possibly 'undefined'.(2532)`

Expected behavior:
Boolean(x) should be treated as equivalent of !!x

Actual behavior:
Using Boolean as a function does not contribute to TS understanding of object existense

Playground Link: https://www.typescriptlang.org/play/?ssl=1&ssc=1&pln=9&pc=31#code/JYOwLgpgTgZghgYwgAgILIN4Chm+XALmRAFcBbAI2iwF8ssEB7EAZzHyPQB9kSQATCDFAR+9Jq0YAbCADopjAOYAKOMgBk6-AH5ZcAJQNmLaXIUqAhBbWb8ewxJMz5S5QCFGpuCFX6NWuHsgA

Related Issues:

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions