Here's a bare-bones example. I have a class whose objects have two properties:
Here's the code:
class Foo {
/**
* @param {Foo?} parent
* @param {string} value */
constructor(parent, value) {
this.parent = parent;
this.value = value;
if(parent) {
let value = parent.value; // vscode says value is undefined
console.log(value); // but when printed it's the expected string
}
}
}
If I mouse over value
on the commented lines, the intellisense popup simply displays let value: undefined
, and gives me the red squiggles if I try to do anything with it.
Seeing as the program behaves as expected, I assume the issue lies in my JSdoc.
Note: the nullability of parent
is not the issue, as this example bears the same problem:
class Foo {
/**
* @param {Foo} parent
* @param {string} value */
constructor(parent, value) {
this.parent = parent;
this.value = value;
let parentValue = parent.value; // vscode says parentValue is undefined
}
}
What am I doing wrong? How can I convince intellisense that value
is definitely a string?
you should define value in Foo
class Foo {
value = ''
/**
* @param {Foo?} parent
* @param {string} value */
constructor(parent, value) {
this.parent = parent;
this.value = value;
if(parent) {
let value = parent.value; // vscode says value is undefined
console.log(value); // but when printed it's the expected string
}
}
}