I create type like the proposal in the socket.io documentation: https://socket.io/docs/v4/typescript/#custom-types-for-each-namespace
then I initialise the io Server with my custom types:
private constructor(server: any) {
this.io = new Server<ClientToServerEvents, ServerToClientEvents, InterServerEvents, SocketData>(server, {
cors: {
origin: 'http://localhost:4200',
credentials: true
}
});
this.io.use(socketAthentication);
this.initializeSocketEvents();
logger.info('Socket server started');
}
exemple of SocketData Type:
export interface SocketData {
appId: string;
moduleId: number;
msg: string;
}
But intellisence in VS Code or typescript compiler to not care about my type. FOr example I can add properties not defined in the socketData interface without any error. I do not have the intellisence when I type socket.data. DO I miss something?
this.io.on('connection', (socket) => {
socket.data.appId = 'TEST';
socket.data.test = 'OK';
}
I fixed this issue for myself some days ago, and it is kinda wonky, but you actually have to specify the type of this.io
in order to tell IntelliSense the actual type of your server/sockets.
Doing it this way should fix the issue:
private constructor(server: any) {
this.io: Server<ClientToServerEvents, ServerToClientEvents, InterServerEvents, SocketData> = new Server<ClientToServerEvents, ServerToClientEvents, InterServerEvents, SocketData>(server, {
cors: {
origin: 'http://localhost:4200',
credentials: true
}
});
this.io.use(socketAthentication);
this.initializeSocketEvents();
logger.info('Socket server started');
}
It seems like typescript doesn't infer a socket from a templated Socket.IO Server, hence cannot find the actual type of the socket.
You can check it by hovering socket
. VS Code doesn't actually shows the templated socket, but actually the default one