Type definitions for Node-RED

I know that there is a types definition for Node-RED. However, I'm having problems with some annoying errors being marked in VScode with type checking turned on and I'd really like to get rid of them because they are false positives.

So does anyone know the correct types for:

  1. The RED object passed into the module.exports = function(RED) { line of the .js file?

  2. The this object (typically assigned to a variable called node) in the callback function for registerTypes in the .js file?

  3. The RED object used in the RED.nodes.registerType line of the .html file?

Thanks. J.

Hi @TotallyInformation

unfortunately the type definitions were created and submitted without any involvement from us. We raised our concerns at the time, but they assured us they would keep them up to date.

As we don't use TypeScript in Node-RED anywhere, it isn't something I've spent any real time with.

Hi Nick, thanks for the response.

While I am not using Typescript, I do use the typescript checking for JavaScript in VScode as it saves me from so many errors and provides loads of interactive help with variables and parameters and such.

Trouble is it is very easy to get terribly confused between the bits of JSDoc that VScode supports and the bits of typescript type checks and intelliscript that it supports.

I have discovered a bit of a work-around for part of the issue, I just wish I knew TS definition structures better so that I could work on a better set for Node-RED.

So in your node's .js file:

// somewhere near the top
/** 
 * @typedef {Object} nrNodeExt Extensions for the nodeInstance object type
 * @property {function} [context] get/set context data. Also .flow and .global contexts
 * @property {function} [on] Event listeners for the node instance ('input', 'close')
 * @property {function} [error] Error log output, also logs to the Editor's debug panel
 * ... obviously there are more ...
 */

// I still don't have a decent type for RED
module.exports = function (RED) {
    /**
     * @typedef {nodeInstance & nrNodeExt} nrNode Combine nodeInstance with additional, optional functions
     */

    function nodeInstance(config) {
        // @ts-ignore - Create the node instance
        RED.nodes.createNode(this, config)

        /** Copy 'this' object in case we need it in context of callbacks of other functions.
         * @type nrNode
         */
        const node = this

        // ... 
    }

    RED.nodes.registerType(nodeName, nodeInstance)

}

This seems to work pretty well though the & operator is not actually supported in the real JSDoc spec since it is borrowed from TS. However, it seems that it works in most IDE's.

Note that any other things you add to the this/node object seem to be handled just fine by VScode.

I just need to find a way to have these things defined externally so they can just be imported.

I am hoping that some of the other, larger brains than mine in the forum who also use VScode can come up with something sensible. Or maybe whoever is maintaining that type definition can be persuaded to expand it a bit more.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.