tc39 / proposal-optional-chaining

Home Page:https://tc39.github.io/proposal-optional-chaining/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Can this effect be achieved?

1316346949 opened this issue · comments

if (!okay) { ... }

okay = null || undefined || '' || false || 0 -> !okay = true

okay = null || undefined || false -> ?.okay = true

okay = 0 || '' -> ?.okay = false

commented

It's pretty hard to understand what you are suggesting, but generally, it sounds like you want to treat false as you would null or undefined.
The intention of optional chaining is to deal with nulls and undefined only. To include false is not in scope of this proposal.
IMO, you are asking for a new, unrelated feature. Personally, I'd find it to be a bit confusing to incorporate Boolean into this proposal, however, if you do feel it is worthy, consider creating a new tc39 proposal.

okay = null || undefined || false -> ?.okay = true

okay = 0 || '' -> ?.okay = false

That can be achieved with the forthcoming nullish-coalescing operator ?? together with an explicit comparison to false:

if ((okay ?? false) === false)

But this has nothing to do with optional chaining.

okay = null || undefined || false -> ?.okay = true
okay = 0 || '' -> ?.okay = false

That can be achieved with the forthcoming nullish-coalescing operator ?? together with an explicit comparison to false:

if ((okay ?? false) === false)

But this has nothing to do with optional chaining.

@claudepache Yes, I mean there are no other proposal solve the problem of “if(!okay){ ...... }”.

Because “okay = ‘’ | okay =0 -> if(okay) or if(!okay) potential problems ”

It's pretty hard to understand what you are suggesting, but generally, it sounds like you want to treat false as you would null or undefined.
The intention of optional chaining is to deal with nulls and undefined only. To include false is not in scope of this proposal.
IMO, you are asking for a new, unrelated feature. Personally, I'd find it to be a bit confusing to incorporate Boolean into this proposal, however, if you do feel it is worthy, consider creating a new tc39 proposal.

@jazeee Sorry, I mean there are no other proposal solve the problem of “if(!okay){ ...... }”.

Because “okay = ‘’ | okay =0 -> if(okay) or if(!okay) potential problems ”

commented

I agree with @claudepache. Their suggestion is the right approach, which handles null, undefined, and false as false, but treats 0, '', and everything else as true.

if(?okay){ ...... } -> okay !== null && okay !== undefined && okay !== false ? dosomething: else do somenthing

commented

You're saying the same sort of thing. It is clearly out of scope.

It is indeed out of the scope of this proposal. Closing as off-topic.