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

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

Closed
maxkomarychev opened this issue Mar 3, 2020 · 4 comments
Closed

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

maxkomarychev opened this issue Mar 3, 2020 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@maxkomarychev
Copy link

maxkomarychev commented Mar 3, 2020

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:

@Ezio1212
Copy link

Ezio1212 commented Mar 3, 2020

i think your issue may couldnot be sensed by when the parameter of Boolean is another function's parameter, like this:

function xxx(a?: A) {
    return Boolean(a) && a.a; // the compiler cannot helps you to understanding this case
}

@maxkomarychev
Copy link
Author

@Ezio1212 I am not sure I follow your suggestion, can you elaborate?

interface A {
    a: number
}

function xxx(a?: A) {
    return a && a?.a
}

function yyy(a?: A) {
    return !!a && a.a
}

function zzz(a?: A) {
    return Boolean(a) && a.a;
}

expressions work exactly the same way in context of a function as they work outside - x && x.prop and !!x && x.prop work perfectly fine while Boolean one does not.

playground: https://www.typescriptlang.org/play/?ssl=1&ssc=1&pln=16&pc=1#code/JYOwLgpgTgZghgYwgAgILIN4Chm+XALmRAFcBbAI2iwF8ssYSQExgB7EZADx4Ao4A-EVQBKTDjxQIYElE5xkAMkX4BAOji16jZqw7IAnkf5C0Y7HmRSZc5AEI7C5fg1aGTFu04AvXyeHmErjWspwAQmxsADYQcCD8Ys5wGgDcWkA

@nmain
Copy link

nmain commented Mar 4, 2020

Duplicate of #16655
My search terms were "boolean constructor type guard"
If you add the overload from #29955 to your example, it works as expected:

Playground

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Mar 4, 2020
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

5 participants