1

I wanted to take some time to "clean" a personal app, to remove most of the warnings, etc.

As stated in the title I use the PhpStorm IDE and I have some warnings "Unresolved variable slug" when I use series.slug. The variable series comes from either a JSON from a PHP Class or after an Ajax call.

Is there a way to indicate an object's properties or to link a js variable to a PHP class (like in Twig)?

P.S. In my "Settings > Languages > JS > Code Quality Tools", I have nothing enabled, I just have the "basic" PhpStorm inspection.

1 Answer 1

2

If you use some object with keys only known in runtime (generated, received through the ajax call, etc.) in your code, there is no way for the IDE to resolve them using static code analysis. But you can let the IDE know what your runtime data looks like. Possible solution using JSDoc annotations:

/**
 * @typedef {Object} series
 * @property {string} slug
 * ... other series props here....
 */
...

/**
 * function that uses series data
 * @param {series} data
 */
function foo (data){...}

See also https://youtrack.jetbrains.com/issue/WEB-17419#comment=27-1058451, https://intellij-support.jetbrains.com/hc/en-us/community/posts/206349469-disable-unresolved-variable-on-json-object-received-by-ajax-call for other possible workarounds

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot, I'll take a look at JSDoc, but it seems it could solve the problem.

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.