I have been going through a Udemy JavaScript course and have had all suggestions pop up in the same way as the instructors video does as they walk through the lecture.
Now we are on classes, and the .includes
method is not being suggested and I don’t understand why this is. I have scoured through many posts here from Stack Overflow as well as the sister sites. My built in JavaScript language extension is not disabled. I have ESLINT and JS es6 Snippets extensions installed. Suggests happen, and the .includes
method will be suggested in another project I have open. In the prior lectures, in the practice files the method also gets suggested. It makes me think that this isn’t a workspace problem or a settings issue.
The GIF is a little cut off at the end but, when I first begin to see if .includes
will be auto suggested for completion, it doesn’t appear. So I complete it.
I press enter, and re-run through that. It appears that once .includes
method has been used, VSCode then now suggests it for autocomplete.
What do I need to do so that the .includes
method is suggested prior to it already being used in the script?
It’s been really helpful for me to have these suggestions come up since I am still learning. I can easily look at the suggestions, see that I can use "x, y, z" and then use Moz JS docs to learn/read how to use it.
Regarding the other possible solutions found when searching this issue, I did follow suggestions. Reloading the workspace/window. Disabling extensions, renabling them. Completely turning off other extensions to rule out conflicts. Different suggestions for jsconfig.json
. Below is my current jsconfig.json.
{
"compilerOptions": {
"target": "es6"
},
"exclude": ["node_modules"]
}
2
Answers
Since JavaScript variables aren’t typed, editor suggestions are based on the editors best attempt at inferring what value the variable holds.
Some of the time there isn’t enough information in the code for the editor to guess the value type, in which case it defaults to assuming
any
:Since
.includes
is only relevant if the value is an array or a string it won’t be suggested if the editor can’t infer the variable to hold one of those two types.You can help the editor by adding a type notation comment (in the form
/** @type {string} */
):You may need to specify the type of "name" before it will suggest ".includes()"
Intellesence tries to avoid suggested methods that will not work for the type.
Edit: Yeah look at JHR’s answer, they explain this in more detail