michaelT
michaelT

Reputation: 1711

Javascript documentation: Returns null or type

I have a function of a js class which can return a String or undefined. Now I want to insert the return statement into the function's documentation.

    /**
     * Test documentation.
     * 
     * @param {(String|Number)} value - A String or a number containing a number.
     * 
     * @returns {(String|undefined)} - Returns a String or undefined.
     */
    nullOrString(value) {
        // ...
    }

But when I call the function, the documentation of the return statement only returns String and not (String | undefined). I also tried it with null instead of undefined, because undefined is returned by default by all functions without a return statement.

So how can I add to my documentation that a undefined or null is returned?

EDIT: Add another test function. Please notice thate these examples are test functions and do not have to make sense.

     /**
     * Test documentation function.
     * 
     * @param {(String|Number)} input - Input String.
     * 
     * @returns {null|undefined} - Returns null when input is numeric, otherwise returns undefined.
     */
    undefinedOrNull(input) {
        if (!isNaN(parseFloat(input))) return null;
        return undefined;
    }

Visual studio code returns:

undefinedOrNull(input: string | number): null

Input String.

Test documentation function.

@returns — - Returns null when input is numeric, otherwise returns undefined.

Upvotes: 9

Views: 12221

Answers (2)

TeNNoX
TeNNoX

Reputation: 2073

be sure to set strictNullChecks to true in your tsconfig.json - otherwise, VSCode will not show | undefined values:

See relevant issue

Upvotes: 5

Jonathan Irwin
Jonathan Irwin

Reputation: 5767

What you have there should work alright

/**
 * 
 * @param value
 * @returns {string|undefined}
 */
function nullOrString(value) {
  // ...
}

No need for the brackets but that shouldn't matter and I think you will want a lowercase string not String.

enter image description here

Upvotes: 7

Related Questions