Skip to content

Can't emit namespace events without arguments in TS #4917

Closed
@BerkieBb

Description

@BerkieBb

Describe the bug
After this change TypeScript doesn't recognize my events without arguments anymore and throws an error when I start my socket.
image

To Reproduce

Socket.IO server version: 4.7.3

Server

// This is not my actual code, but my code to reproduce my issue
import express from 'express';
import http from 'http';
import { Namespace, Server } from 'socket.io';
const app = express();
const server = http.createServer(app);
const io = new Server(server);

app.get('/', (_, res) => {
  res.send('<h1>Hello world</h1><script src="https://cdn.socket.io/4.7.3/socket.io.min.js"></script><script>var socket = io();  socket.on(\'test\', () => {console.log(\'test triggered\');});</script>');
});

declare namespace SocketServer {
  interface ClientEvents {
    ['test']: () => void;
    ['with-args']: (arg: string) => void;
    ['with-args-two']: (arg: string) => void;
  }
}

const namespace: Namespace<SocketServer.ClientEvents> = io.of('/server');

namespace.on('connection', () => {
  console.log('a user connected');
  namespace.emit('test');
});

server.listen(3000, () => {
  console.log('listening on *:3000');
});

Client

Don't have one for my reproduction code and already in the HTML sent when the app is connected.

Expected behavior
For the event to get recognized by TypeScript even though no arguments were provided and for it to compile without any issues.

Platform:

  • Device: Desktop
  • OS: Windows 10

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions