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
extended diagnostic for non-nullable optional is wrong #46918
Comments
@tiberiuzuld could you please share more details / reproduction scenario? We were briefly looking into it but the types and assigned values don't match thus making the problem confusing. Your type is written as In short - the type, assigned value and expression don't seem to match / make sense together so I'm assuming there is some typo / missing info here. |
Hello @pkozlowski-opensource , |
Thnx for the update @tiberiuzuld - yes indeed, it looks like a valid bug report, thnx! |
Hmm I was investigating the issue a bit some more. Don't know if you want to include this as a bug. |
I found another case where I think the bug is valid. |
OK, so the first case is definitively not a bug and works as expected. The second |
@tiberiuzuld could you please update the issue description to remove references to the first case (as this works as intended?) |
@pkozlowski-opensource |
Workaround possible with making a getter/method in the class ts: get firstElement(): {a: 'value'} | undefined {
return this.optionalArrayElement[0];
}
getElement(index: number): {a: 'value'} | undefined {
return this.optionalArrayElement[index];
} <button [disabled]="firstElement?.a"></button>
<button [disabled]="getElement(0)?.a"></button> |
we had a similar issue, see #44859 (comment) - when |
I'd also like to turn off this warning. |
I also think that this NG8107 warning is broken. For example:
|
@nunoarruda can you please share a runnable reproduction? As for the indexed access false positive: this happens because TypeScript doesn't include |
@JoostK it looks like it is working fine in an isolated demo: |
|
Please do not do this. This particular rule can be disabled like any other extended diagnostic, as documented here: https://angular.io/extended-diagnostics#configuration. The rule name is |
|
Can verify that this did fix it for me. Seems to require at least angular 14.1. |
Hello. Got the same problem all over my application where things can clearly be null or undefined but I receive the warning regardless. Two examples bellow: Angular CLI: 14.1.0 Angular: 14.1.0 Package Version@angular-devkit/architect 0.1401.0 |
@robleka the first case you mentioned is because you defined (Regarding the second case, I think it's because item is defined as |
Thanks, that fixed the first issue. But the second one is still strange to me as "item" is optional. Yes, it can be of any type, but it still is optional, it might not exist or it may be null. |
Tip: watch out for optional properties inside TS: Template:
|
Got the same warning:
After I "fixed" the warning it, got following error:
|
@ghovmand try |
It appears there is some confusion which optional chain is redundant. It's always the one to the left of the underlined property. If I recall correctly it wasn't possible to highlight just the operator, hence we now highlight the property name, but perhaps this can be improved as well. I'm currently on vacation though, so won't be able to look into this at the moment. |
Thanks, that solved the issue for me; always look to the left :) |
An example of how to suppress this check right now (it's not obvious from previous comments): "angularCompilerOptions": {
"extendedDiagnostics": {
"checks": {
"optionalChainNotNullable": "suppress"
}
}
}
|
I am seeing this warning quite a lot with this pattern:
Which is quite common, as i tend to fetch the data in a resolver and assign it to the variable in the constructor.
I definitely don't want to disable these checks, as they are definitely useful. But in this case it feels like angular should handle this case internally. EDIT: |
This is effectively exactly the same as:
In other words, in this class There's no way to automatically mark things like |
@e-oz Thank you work for me on the angular 14 version |
…cesses TypeScript's type system does not include `undefined` in the type of an indexed access, whereas array index accesses may be expected to be out-of-bounds. This commit updates the extended diagnostic for unnecessary optional chains to ignore indexed accesses. Closes angular#46918
I meet the same bug One of the possible solutions is to replace the |
this worked for me, thanks! |
The same error occurred to me, have suppressed this warning for now by using #46918 (comment) |
Which @angular/* package(s) are the source of the bug?
core
Is this a regression?
No
Description
Having the case below produces the warning for optional chain operation.
Because of
!
it will always return a boolean to the input.Relates to the new feature #46686
#44870
Also can we have a
angularCompilerOptions
to disable this warnings?First case:
Second case:
Please provide a link to a minimal reproduction of the bug
https://stackblitz.com/edit/angular-r3n3ec?file=src/app/app.component.ts
Please provide the exception or error you saw
Please provide the environment you discovered this bug in (run
ng version
)Anything else?
No response
The text was updated successfully, but these errors were encountered: