skip to Main Content

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


  1. Chosen as BEST ANSWER

    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 disables nullish unions in this case.

    So after putting it in my jsconfig.json, and reloading VSCode it now displays the union correctly on the tooltip.


  2. 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, but it is documented). 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search