Sunday, 14 May 2017

Typescript: correctly typing a SockJS Session

I'm fairly new to Typescript, and I'm refactoring a server I help maintain to use it. I've been trying to declare a type for SockJS server sessions, but so far I've been getting nowhere. I've tried defining the type these ways:

/* @typedef {[require('sockjs/lib/transport').Session.prototype]} [Socket] */

/* @typedef {[require('sockjs/lib/transport').Session]} [Session] */
/* @property {prototype: any} [Socket] */

// Out of desperation
/* @typedef {[any, string]} [Socket] */

Yet I keep getting errors like these:

server.js (176,13): Cannot find name 'Socket'. (2304)
server.js (181,11): Property 'destroy' does not exist on type '[any, string]'. (2339)
server.js (203,18): Parameter 'socket' implicitly has an 'any' type. (7006)

This is the only part of the refactor that's been giving me any trouble. I use SockJS sessions throughout the file, so I feel it'd make sense to give it its own type rather than giving up and using any instead. Is there a way of going about declaring it that I'm missing? Bear in mind I'm only able to use JSDoc to accomplish this to stick to the project's style guide, for the time being at least.



via kpimov

No comments:

Post a Comment