#Need help typing custom properties for a websocket connection

2 messages · Page 1 of 1 (latest)

tired meteor
#

with the "ws" package for websockets, you can add custom properties to each connection

heres an official untyped example with the "isAlive" property: https://github.com/websockets/ws#how-to-detect-and-close-broken-connections

if you try to do anything like this in typescript with the "@types/ws" package, you will get an error saying that the property you are trying to access doesn't exist, which is understandable

this github issue mentions a couple possible solutions that haven't worked for me:
https://github.com/websockets/ws/issues/1517

i made a minimal stackblitz that tries these solutions but still gets type errors:
https://stackblitz.com/edit/stackblitz-starters-qyl7ku?description=Starter project for Node.js, a JavaScript runtime built on Chrome's V8 JavaScript engine&file=package.json,src%2Findex.ts,src%2Fcustom-ws-2.d.ts,src%2Fcustom-ws-1.d.ts&title=node.new Starter

any help appreciated

faint cove
#

Could you just wrap the type?

interface MyWebSocket {
  ws: TheSocket
  customProp: string
}