Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type information to Socket.data #4159

Merged
merged 4 commits into from
Nov 8, 2021

Conversation

backmeupplz
Copy link
Contributor

The kind of change this PR does introduce

  • a bug fix
  • a new feature
  • an update to the documentation
  • a code change that improves performance
  • other

Current behavior

Socket.data is any

New behavior

Socket.data is any by default but Partial<ProvidedType> if a type is provided in the Server and/or Socket constructors

Other information (e.g. related issues)

I had to wrap the provided type in Partial because we are initializing the Socket.data with {}. I did not want to alter this behavior but instead I just wanted to provide a simple way to type the Socket.data field. This fixes #4155.

@backmeupplz backmeupplz changed the title Add type information to socket.data Add type information to Socket.data Nov 6, 2021
@backmeupplz
Copy link
Contributor Author

I'm also not sure if I missed any spots where the types should be added. Maybe @MaximeKjaer could take a look?

@darrachequesne darrachequesne merged commit fe8730c into socketio:master Nov 8, 2021
@darrachequesne
Copy link
Member

@backmeupplz awesome, thanks a lot 👍

dzad pushed a commit to dzad/socket.io that referenced this pull request May 29, 2023
Usage:

```js
interface SocketData {
  name: string;
  age: number;
}

const io = new Server<ClientToServerEvents, ServerToClientEvents, InterServerEvents, SocketData>();

io.on("connection", (socket) => {
  socket.data.name = "john";
  socket.data.age = 42;
});
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Manually add types to socket.data
2 participants