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

SocketData being wrapped with Partial can create the need to use non-null assertions #4537

Closed
navidmafi opened this issue Nov 30, 2022 · 1 comment
Labels
enhancement New feature or request
Milestone

Comments

@navidmafi
Copy link

Is your feature request related to a problem? Please describe.

Socket.IO allows to add arbitrary data to Socket instance with Socket.data which is very nice and handy. the problem arises when using this data in a type-safe manner.
Please take a look at the example below:

In this example, the middleware ensures that the socket connection is authenticated and if it is, it adds some data to socket instance for later usage.
As you can see, the Server object is typed with ISocketData which looks like this :

export interface ISocketData {
        userID: string;
        userName: string;
        isEmailVerified: boolean;
        joinedRoomUUID: string | null;
        joinedRoomInviteCode: string | null;
    }

image

But since Socket.data type is internally wrapped with Partial, undefined type will be added to all of the properties.
This can causes the need to use non-null assertion or any type of TypeAssertion when using any of the properties in Socket.data, since of course Typescript wouldn't know the logical context.

image

Describe the solution you'd like

Not wrapping SocketData with Partial internally. Current codebases will work fine and properties can still have the undefined type by just adding it in type of the value passed to SocketData like below if the business logic or anything requires them to have the undefined type.

image

Describe alternatives you've considered
Using manual assertions introduced in TS 3.7. please check the links below with the first one being the same example said here.

https://twitter.com/NavidMafi/status/1597884685551513600
https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions

Additional context
image

image

@navidmafi navidmafi added the enhancement New feature or request label Nov 30, 2022
darrachequesne pushed a commit that referenced this issue Jun 20, 2023
Wrapping SocketData with Partial causes issues when reading data even
if you've made sure to pass all values. If someone want to make their
type Partial or make only a few properties optional, they can do so in
their own type instead.

Related: #4537
@darrachequesne darrachequesne added this to the 4.7.0 milestone Jun 20, 2023
@darrachequesne
Copy link
Member

This should be fixed by e5c62ca, included in version 4.7.0.

Please reopen if needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants