1

Consider this ES6 code:

class Person {
    constructor(first, last) {
        let fixed = this.normalize(first, last);
        this.first = fixed[0];
        this.last = fixed[1];
    }

    normalize(first, last) {
        return [first.toUpperCase(), last.toUpperCase()];
    }
}

PhpStorm/WebStorm has no problem recognizing that first and last are properties of the class when I later use them:

enter image description here

Now if I modify the constructor to use the destructuring assignment syntax:

constructor(first, last) {
    [this.first, this.last] = this.normalize(first, last);
}

The code executes without error, but the IDE can no longer see the properties:

enter image description here

enter image description here

Is this a problem with my code? My IDE settings? I use PhpStorm 2017.2.4

1
  • 4
    "Is this a problem with my code?' No, your code is fine. Apparently PhpStorm 2017.2.4 just doesn't understand destructuring assignment in the constructor (yet). Commented Oct 12, 2017 at 13:03

1 Answer 1

2

You can manually tell PHPStorm that these properties should be assumed to exist on Person-linked objects by adding a JSDoc to the class:

/**
 * @property {String} first
 * @property {String} last
 */
class Person {
    // ...
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.