Skip to content

Cannot set socket.request to null because the property is read-only #696

Closed
@jonathanperret

Description

@jonathanperret

Describe the bug

The Socket.IO documentation suggests setting the request property of the socket object received with the connection event to null. But the types as defined do not allow setting that property from a TypeScript program.

To Reproduce

Note that this is a Socket.IO program, because it is taken from the Socket.IO documentation, but it seems clear that the issue comes from the Socket type declared in Engine.IO, which is why I'm opening this issue here.

Socket.IO server version: 4.7.4

Server

import { Server } from "socket.io";

const io = new Server(8080);

io.on("connection", (socket) => {
    console.log(`connect ${socket.id}`);
    socket.request = null;
});

When compiling with tsc server.ts here is the result:

server.ts:7:12 - error TS2540: Cannot assign to 'request' because it is a read-only property.

7     socket.request = null;
             ~~~~~~~

Found 1 error in server.ts:7

Expected behavior

The request property should not be marked read-only, in order to allow this code to compile.

Workaround

Bypassing the restriction works:

(socket as any).request = null;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions