-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
"This binary expression is never nullish" errors due to the way I format my code #59546
Comments
I wouldn't call this a formatting issue, since the line breaks aren't involved. Writing it all on the same line doesn't change the error. Note that whether or not this is intentional behavior, the error message is bizarre. Right now it's flagging |
That's true. I guess I was a little hyperfocused on the formatting aspect because I wouldn't do this kind of thing a single line, since it's just noise in that case |
And you can't even work around it using a type assertion, which IMO would be a suitable solution to the OPs issue. |
The error message needs to be fixed but I'd still consider this a correctly-issued error. This doesn't seem to be an idiomatic pattern and honestly, as a human, that code does look like it has a bug. It's not really distinguishable from other cases which absolutely do represent bugs. As a workaround you could write something like const seeBelow = undefined;
const p = seeBelow
?? a
?? b
?? c; since the check is entirely syntactic on
|
Since you've added Help Wanted and put it in the Backlog, does that mean it won't be fixed in 5.6.0 unless I PR to fix it? I can try, I suppose. Regarding the comment: In the one project I had to port over to 5.6.0-beta due to a teammate, I changed about 30 instances of code like this to use a new I have been using TypeScript and doing this for a number of years and would not be exaggerating to say there are hundreds of instances of this in my code. I would really rather not have to change all of them. It's probably at least a thousand if we're including the In an ideal world, I would be able to put nothing on the first line and have So since JavaScript will probably never support this, I've been doing this alternative. (And like all programming things, if you haven't been doing it you're not used to code like this so it looks confusing. I promise, for people doing this consistently, it's not!) As far as this not being idiomatic β when exactly does something become idiomatic? SQL commonly has people write |
I imagine the PR that would be accepted is a correction on the error message. Setting aside the specific error message, that there is an error under these circumstance is "working as intended." Branching on a known/constant condition is indicative of a mistake and should be called out. It wouldn't make sense to get rid of that useful functionality to suit a coding style. |
"idiomatic" means "in common use". We ran this check on 800 repos and found zero instances of this pattern; everything the check did find was a bona fide bug. At some point it's worth the 0.1% false positive rate to find the other 99.9%. |
I've cloned the latest commit and I've been playing with const p01 = null ?? 1; // This expression is always nullish.
const p02 = null ?? null; // This expression is always nullish.
const p10 = cond ?? null ?? 1; // OK
const p11 = cond ?? null ?? null; // OK
const p12 = cond ?? (null ?? 1); // This expression is always nullish.
const p13 = cond ?? (null ?? null); // This expression is always nullish. To me it seems like the actually useful error would be on |
This also doesn't error but probably should? declare let opt: number | undefined;
declare let opt2: number | undefined;
const p19 = opt ?? (opt2 ? null : undefined) ?? 1; // OK |
Well, I have it working as I think it should locally, so I could make a PR. But I think we're still not on the same page so maybe this isn't helpful? Thoughts appreciated though Note: Making the middle always-nullish and never-nullish operands error involved checking the right operand as well as the left operand. I think it makes sense that it must be done though? Because given a situation where the first left operand is Edit: I went ahead and just made the PR because I wanted to have done the whole process once in case I need the knowledge another time in the future |
I encountered this same in my project.
we had It just seems slightly strange to see the special handling for |
From the PR description:
|
π Search Terms
This binary expression is never nullish
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play/?ts=5.6.0-beta#code/KYOwrgtgBACghgJwC4HkBmAROBPKBvAWACgBIAOQEsBzACySQomCgF4oAmKAKi6gAYANMRJYA7iFYduvAIxDSAZTAgEFAM7M2nHh3kicDJpO28AzHqUgNSY9KgAWPRjBqA1rZ0BWecICCAG38sbENNKDEJAB8oS1UNKGjg0ISY5Ws9AP9KWnpGMOc3FOy6UIz-URw1SUykvJTM4tymeQBfYmIAE2AAY39EZm6AeysbKgRBsAAHAC4oACUewYQOgB54ZHRggShwCAAjYAQAbQBdU5TlLrQKEGAOgD4Abk6evoQB4bUbScRUTBxZus-sFnkRiEMRlBrFU2JdgNdbh1hAB6ZFQJAIXBjODgN5QH7IKCDNBQDo4KDXBBfYQAfhpUDGE0mRwJwJwJxRaKQNFAUG6PO67goJO5zGxuMQ+N+RJJZNw6igONwrJlO2oJTy2xxHSgwqhg3RmMVgTVOShP3Ealp9IAFEDNjgAHQNdVNZgAMilG3+uHpjKmR3tPudgUaoROUFmcIRdwAlJz0TyJPyekKRTyGQgcWA8SriaTyQqlV6bPm5aVFSAdXq1AaMbg4Ca5ea4JbrVA7b8HdgQ0EDHVPazu1A-eMA0Hgr3akwI1Gq-CbnGE2hG-49nBBeiDauC7g1BarO3-cyJ06AhVsGoTqCgA
π» Code
π Actual behavior
My codebases have a number of instances of things similar to the above β
undefined
is used to put the actual potential results each on their own line, all formatted the same way. However this no longer works in TS 5.6.0 due to it throwing an error. I think this is an over-eager error message.π Expected behavior
I should be able to format the code how I want to still
Additional information about the issue
Other examples of code in my codebases broken by this
Also, this still works for
true &&
conditions andfalse ||
conditions β would be nice if it stayed consistent:The text was updated successfully, but these errors were encountered: