1

A perhaps pedantic question: I'm enjoying the type checking of IntelliJ and would like to know how to aid it in resolving more complex types.

In my NodeJS project I pass a SocketIO namespace into the constructor of a controller class.

I'd like IntellJ's code analysis tool to know that the SocketIO namespace inherits from EventEmitter (or at least implements its methods).

// In another file (controller.js)
function MyController(namespace){
  /** @var ???? **/
  this.ns = namespace;
}

MyController.prototype.doSomething = function(){
// ....
this.ns.emit('my-event'); // IntelliJ complains (rightly) that it doesn't know about emit()
}

module.exports = MyController;

// In my main file (index.js)
var Controller = require('./controller);
var http = require('http');
var io = require('socket.io')(http.createServer);  
var ns = io.of('/namespace');

var ctrl = new Controller(ns);
ctrl.doSomething();

So is there a syntax for commenting JS variables (perhaps specific to Node) where you can use the module name as a namespace such that static analysis tools like this can resolve them correctly?

Within SocketIO the main class is simply 'Server' which could be easily confused for many other similar Server classes and I've not quite dug deep enough to find the class that's returned in response to calling io.of(...)

Any help or pointers greatly appreciated!

Duncan
  • 858
  • 1
  • 11
  • 29

0 Answers0