Search code examples
javascriptintellisensejsdoctyping

JSDOC - JS How do I overload a method?


I'm struggling to document an overload to a method, though it works fine for object.

This works:

const test = {
    /** 
     * @type {{
     * (bar: string) => boolean
     * (bar: number) => string
     * }} 
     * */
    foo: (bar) => {
        // something...
    }
}

Code with working intellisense

Doesn't work:

class Test {
    /** 
     * @type {{
     * (bar: string) => boolean
     * (bar: number) => string
     * }} 
     * */
    foo(bar) {
        // something...
    }
}

Code with not working intellisense


Solution

  • TypeScript 5 introduces a new @overload tag, so you will be able to do something like:

    class Test {
        /** 
         * @overload
         * @param {string} bar
         * @returns {boolean}
         *
         * @overload
         * @param {number} bar
         * @returns {string}
         *
         * @param {string | number} bar
         * @returns {boolean | string}
         */
        foo(bar) {
            // something...
        }
    }
    

    See:

    https://devblogs.microsoft.com/typescript/announcing-typescript-5-0-beta/#overload-support-in-jsdoc