Search code examples
angularwebstorm

Webstorm: What does "Must be lvalue" mean


While writing my angular application, I got this "strange" error message:

"Must be lvalue" Error

The code runs perfectly fine and I couldn't find any documentation for this error message. What does it mean?

The code is from : https://stackoverflow.com/a/45144391/639035 which is an accepted answer with 15 up votes.

Code:

<input placeholder="TEST"
       [ngModel]="phone_numbers && phone_numbers[0]?.full_number"
       (ngModelChange)="phone_numbers?.length && phone_numbers[0].full_number=$event">

Error Message:

Must be lvalue

Must be lvalue


Solution

  • This is how webstorm inspection by using JSAnnotator works.

    Try creating simple js file with the following code:

    let a,b;
    a && b=1;
    

    enter image description here

    It would be better if that error would sound like:

    ReferenceError: invalid assignment left-hand side

    See also issue

    Webstorm warns us that this code is unusual case and we can be wrong with writting it, for example

    if (a === 1 && b = 2) {
                     /\
       Seems it should be == or ===. 
       So it's easy to make such mistake like this
    

    How can we suppress this error?

    • rewrite code in the following way

    (ngModelChange)="phone_numbers?.length ? phone_numbers[0]['full_number']=$event : null"
    
    • supress JSAnnotator for the input element

    <!--suppress JSAnnotator -->
    <input placeholder="TEST"
           [ngModel]="phone_numbers && phone_numbers[0]?.full_number"
           (ngModelChange)="phone_numbers?.length && phone_numbers[0]['full_number']=$event">
       
    

    Again, it's just a warning and it's up to you how to deal with it.