-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
[@typescript-eslint/no-unnecessary-condition] False-negative with ??=
#3553
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
The rule just doesn't support these operators yet. |
Just to be clear for posterity's sake - the assignment operators are equivalent to the expression const result: { [key: number]: number } = {};
result[1] ??= 1;
result[1] ?? (result[1] = 1);
declare let foo: null;
foo ||= null;
foo || (foo = null);
declare let bar: {};
bar &&= 1;
bar && (bar = 1); But all of those cases are supported - so it's a no brainer to add this |
I think I found a bug when tsconfig’s // tsconfig:compilerOptions.exactOptionalPropertyTypes = true
const obj: { prop?: [number] } = {};
obj.prop;
// ^? (property) prop?: [number] | undefined
obj.prop = [42];
// ^? (property) prop?: [number] I have a function that sets the property once and returns it on the first call, but subsequent calls only return it without setting it: function setOnce(value: number): [number] {
return obj.prop ??= [value]; // ! @typescript-eslint/no-unnecessary-condition: "Unnecessary conditional, expected left-hand side of `??` operator to be possibly null or undefined."
}
setOnce(1); //=> [1]
setOnce(2); //=> [1]
obj.prop.push(3);
setOnce(1); //=> [1, 3] The problem here is that typescript-eslint thinks I’m not sure what the right solution is. We want to keep |
Repro
Expected Result
An error of that rule to be reported.
Actual Result
No error.
Additional Info
This does result in an error so the other code should too.
Versions
@typescript-eslint/eslint-plugin
4.27.0
@typescript-eslint/parser
4.27.0
TypeScript
4.3.4
ESLint
7.28.0
node
16.3.0
The text was updated successfully, but these errors were encountered: