Search code examples
javascripttypescriptvisual-studio-codejsdoc

Why does the hover for my JSDoc type definition unioning with null in VS Code not show null as part of the union?


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)} */

Solution

  • 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:

    1. The strict property isn't directly the property of interest. The real config property of interest is strictNullChecks, which defaults to true if strict is set to true, and otherwise defaults to false.

    2. 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, though there are traces/hints of its existence in relevant documentation). The particular setting of interest here is js/ts.implicitProjectConfig.strictNullChecks, which defaults to true.

    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 to false.