Search code examples
javascriptfunctionreturn-type

Define a functions return type so it'll be shown in the tooltip when writing a function call


I want to be able to set a functions return type so that my IDE can apply Intellisense.

e.g. i have a function that takes an object and creates/returns a HTMLElement from that. I want that when i call that function like that:

var myElement = document.createElementWithProperties({elementType: 'div', id: 'myElement',style:{background: 'blue'}});

On the next Line when i enter "myElement." it would suggest me for example appendChild.

document.createElement example


Solution

  • You could decorate your function with a JSDoc documentation block. For example some thing like this:

    /**
     * Creates an element with the specified properties.
     *
     * @param {object} The properties to create the element with.
     * @return {HTMLElement} The HTML element.
     */
    function createElementWithProperties(obj) {
        // Do stuff!
    }
    

    Note: This doesn't seem to work, in Visual Studio Code, when extending the native document object but should work fine as a stand alone function or as part of a user defined object.