Question

I'm trying to convert a piece of code to TypeScript and I've run into an issue with a Buffer that I use with a UDP socket.

The code I'm working with looks like this:

/// <reference path="../node_modules/DefinitelyTyped/node/node.d.ts" />
import dgram = require( "dgram" );

var udpServer:dgram.Socket = dgram.createSocket( "udp4" );

var message = new Buffer( "foo" );
udpServer.send( message, 0, message.length, 1337, "255.255.255.255" );

WebStorm complains about message with the error Argument type Buffer is not assignable to argument type NodeBuffer.

I understand that this is due to the fact that Socket.send() requires a NodeBuffer as the first argument and I'm supplying a Buffer. But how can I overcome this?

I can't use NodeBuffer as the type of message, that just leads to different errors.

var message:NodeBuffer = new Buffer( "foo" );

Will lead to Initializer type Buffer is not assignable to variable type NodeBuffer

var message:NodeBuffer = new NodeBuffer( "foo" );

Will lead to Argument types do not match parameters.

This is confusing me due to the examples given at http://nodejs.org/api/dgram.html which worked for me in plain JavaScript.

So, how do I properly construct a buffer that I can send over my UDP socket?

Was it helpful?

Solution

The problem was caused by having the Node.js v0.8.16 Core Modules library enabled in the JavaScript settings of WebStorm.

This confused WebStorm in regards to which type the Buffer actually is. Disabling the library would resolve the error hint.

Sadly, that isn't really an option for us as the project uses both Node.js with JavaScript and TypeScript code. But at least I now know what the problem is.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top