-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Bug: [no-unnecessary-condition] false positive with ??=
operator and exactOptionalPropertyTypes
compiler option
#6635
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
Comments
|
* Shorten long recurring events to end date * Add missing imports, switch to named imports * Update snapshots * Fail on ESLint warnings * Increase max diff lines for Vitest * Downgrade packages for false positive Ref: typescript-eslint/typescript-eslint#6632 Ref: typescript-eslint/typescript-eslint#6635 * Fix import * Revert "Fix import" This reverts commit d194a25.
Filed a bug with TS - microsoft/TypeScript#53305 If not, then we either must implement our own logic to determine if the LHS might be an exact optional type or not, or we might just disable checks on |
@bradzacher — I believe TS is working as intended since after assignment it will be function ee() {
xx.prop = 1;
// ^?
// typeof xx.prop === number ❌
} |
@chharvey if it was working as intended then
xx.prop += 1
// ^?
// typeof xx.prop === number, because the result of the assignment operator will always be number But that's not what we see from TS - we see that TS correctly shows that You need to separate the read from the write!
TS does appear to do this - but from the looks of it TS also does some funny stuff with the type of a variable that has a union type at write time - it appears to invalidate the refinements on the variable at specific write times. |
This is filling up my code with a bunch of |
This issue hits our team really bad. We're currently working around this with a local patch via diff --git a/patches/@typescript-eslint__eslint-plugin@5.58.0.patch b/patches/@typescript-eslint__eslint-plugin@5.58.0.patch
new file mode 100644
index 000000000..c38427dcd
--- /dev/null
+++ b/patches/@typescript-eslint__eslint-plugin@5.58.0.patch
@@ -0,0 +1,12 @@
+diff --git a/dist/rules/no-unnecessary-condition.js b/dist/rules/no-unnecessary-condition.js
+index 6d36b811889c22e0b6dc61beb40a82671fd47ad4..d0f06ace52ecbffe4b571ebc9d05450dca15a1fb 100644
+--- a/dist/rules/no-unnecessary-condition.js
++++ b/dist/rules/no-unnecessary-condition.js
+@@ -479,7 +479,6 @@ exports.default = (0, util_1.createRule)({
+ }
+ }
+ return {
+- AssignmentExpression: checkAssignmentExpression,
+ BinaryExpression: checkIfBinaryExpressionIsNecessaryConditional,
+ CallExpression: checkCallExpression,
+ ConditionalExpression: (node) => checkNode(node.test), |
It seems like the TypeScript PR microsoft/TypeScript#54777 was merged, which closed the issue in the TypeScript repo: Will this mean that this issue will "fix itself" once the TS PR changes are released in a version of TypeScript (eg. maybe 5.2)? |
Can you confirm it's related ? |
Running with 5.3-beta now seems to still trigger the warning for me, so I'm assuming some fix is also needed here? |
Before You File a Bug Report Please Confirm You Have Done The Following...
Playground Link
https://typescript-eslint.io/play/#ts=4.9.5&sourceType=module&code=MYewdgzgLgBCBGArAXDA3jADgJxJg-KgNpgCuAtvAKbYC6MAvjALzoMDcAUJwGaljAoAS3AwIVKAHkBVABQA3AIYAbUlVRlKNAJTFN1Ouk4wY2CaWxg4SAHQ48MfPlZElqqrXYwA9N5gBCGAABKABPTCoIYGwhTCgAWkjlITAobzAQeP4wKmBIiEVsUPjQMAATIWFwVAAiAFUwHLyIAqKYUoqqsBUAGhgqAA8IwSoymGUqHgSAC0VysSEyqjgeGAADJzW4COxFKBBsGH2YaiwQFqF4ZVCYMmVlOEP+JZ4U0ZsazgZOIA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Jge1tiacTJTIAhtEK0yHJgBNK+SpPRRE0aB2iRwYAL4gtQA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoAkNZOgB4CG0euBxTqACprIljwBPACpCBZSnkwBXdABpwEKCWnJWAORmpUAYQAW6aAGtJYaXKUBfEFaA
Repro Code
ESLint Config
tsconfig
Expected Result
I expected the expression
obj.prop ??= [value]
to not report an error.Since the property hasn’t been set yet, it is actually undefined at runtime.
Actual Result
The expression
obj.prop ??= [value]
is reported as an error.typescript-eslint thinks
obj.prop
is non-nullish sinceexactOptionalPropertyTypes
is on. We don’t want to turn it off because we still don’t want to allow statements likeobj.prop = undefined;
.Additional Info
Related: #3553, #5344, #6632
The text was updated successfully, but these errors were encountered: