I have the following LinkedList
class definition using JSDoc and jsconfig.json
for type checking in VSCode. In it it should display the type of value
and next
as union types with undefined, however it does not, only showing their default type.
class LinkedListNode {
/**
* @param {number | undefined} value
* @param {LinkedListNode | undefined} next
*/
constructor(value, next) {
/** @type {number} */
this.value = value ?? 0;
/** @type {LinkedListNode | null} */
this.next = next ?? null;
}
}
Taking the following line as an example, thought it applies to all the unions.
/** @type {LinkedListNode | null} */
this.next = next ?? null;
The hover message on this.next
is currently, not showing correctly:
(property) LinkedListNode.next: LinkedListNode
What I wish to see:
(property) LinkedListNode.next: LinkedListNode | null
I have tried the following, none of which have worked:
/** @type {LinkedListNode | null} */
/** @type {?LinkedListNode} */
/** @type {(LinkedListNode|null)} */
2
Answers
As it turns out, I had forgotten something important.
In my
jsconfig.json
I had forgotten to put in"strict": true
. Which turns out to disablesnullish
unions in this case.So after putting it in my
jsconfig.json
, and reloading VSCode it now displays the union correctly on the tooltip.The asker of this question already gave a sort of correct answer, which is that you can change this behaviour in a jsconfig.json‘s compiler options using the
strict
property.But they’ve missed two important things:
The
strict
property isn’t directly the property of interest. The real config property of interest isstrictNullChecks
, which defaults totrue
ifstrict
is set totrue
, and otherwise defaults tofalse
.jsconfig.json actually has implicit defaults that are configurable in VS Code settings- specifically, those that start with "
js/ts.implicitProjectConfig.
" (a bit of an obscure fact, but it is documented). The particular setting of interest here isjs/ts.implicitProjectConfig.strictNullChecks
, which defaults totrue
.So if you piece that together with the problem description in the question, the first thing to check is that you haven’t accidentally set the
js/ts.implicitProjectConfig.strictNullChecks
VS Code setting tofalse
.