socket.io icon indicating copy to clipboard operation
socket.io copied to clipboard

Unable to type middleware properly in typescript

Open janusqa opened this issue 2 years ago • 3 comments

Describe the bug (socket: Socket, next: (err?:ExtendedError)=> void When trying to type a middleware function for socket IO the above gives an error that ExtendedError is only defined internally

To Reproduce

in some file create your middle ware const myMiddlewarefunc = (ocket: Socket, next: (err?:ExtendedError)=> void) = > { // do something }

in main file probably index.ts io.use(myMiddlewarefunc)

Socket.IO server version: x.y.z "socket.io": "^4.7.2",

janusqa avatar Aug 14 '23 19:08 janusqa

I have the same problem.

Fabioni avatar Jul 08 '24 20:07 Fabioni

Hi! ExtendedError is indeed not currently exported.

As a workaround, you can simply use an Error type:

const myMiddlewareFunc = (socket: Socket, next: (err?: Error) => void) => {
  // do something
}

darrachequesne avatar Jul 09 '24 09:07 darrachequesne

@darrachequesne hey, the problem is, the normal error type does not have the data property.

Fabioni avatar Jul 09 '24 15:07 Fabioni

For future readers:

The ExtendedError type is now exported: https://github.com/socketio/socket.io/commit/32c761f02fb67625297ed6644f744b7647662e65

Included in [email protected].

darrachequesne avatar Oct 22 '24 06:10 darrachequesne