Non-null assertion operator whitespace allowances can create confusing conditional statements #60518
Labels
Experience Enhancement
Noncontroversial enhancements
Help Wanted
You can do this
Suggestion
An idea for TypeScript
Milestone
🔎 Search Terms
You can write a non-null assertion operator with whitespace after a variable name and no white space before certain keywords such as
in
andinstanceof
and the type checker doesn't fail but instead strips the operator creating the opposite condition as it appears.To a someone not familiar with TypeScript/JavaScript
key !in obj
reads likeif key not in obj
. To them this might be if the tsc treatedkey !== 'a'
askey == 'a'
🕗 Version & Regression Information
⏯ Playground Link
https://www.typescriptlang.org/play/?ts=5.8.0-dev.20241116#code/MYewdgzgLgBAZiEAuGBvAhgfhdATgSzAHMBfGAXjXRQHIAjdXGkgbgChRJYBrAUwE8KMGuhox0EGJ2js2+ODAAUfQQEJC8RAEo0bGFPAQQAG14A6YyCKKEIANoqAulvYk2chTcQx1XdGGBeEAUAeToAK15gKB1UPQNIE3NLayh+AAcghVsXNhIgA
💻 Code
🙁 Actual behavior
The TSC stripped the non-null assertion, the operator should not be considered a non-null operator at all in this situation.
🙂 Expected behavior
I would expect a syntax error in this situation.
Additional information about the issue
This is a real problem not just hypothetical, I noticed a discussion started by a person learning typescript complaining about how their code was evaluating true regardless of the "negation operator" being used before the
in
keyword.The text was updated successfully, but these errors were encountered: