#TypeScript ESLint: "Unsafe assignment of an error typed value" when using Socket in NestJS WebSocket

16 messages · Page 1 of 1 (latest)

merry shale
#

I'm working on a NestJS WebSocket gateway and running into a TypeScript ESLint error when trying to use the Socket type from socket.io. 😵‍💫

upbeat leaf
#

/* eslint-disable @typescript-eslint/no-unsafe-member-access */
add top of the file these line

upbeat leaf
dreamy pecan
#

why did u write there?

upbeat leaf
#

it goona disable that property of eslint for that file

merry shale
# dreamy pecan Hi, did u fix issue?

It’s still not fixed.
I’d like to know if there are any solutions other than disabling this rule.

I don’t understand why the original code throws an error. From what I read in the documentation, the first parameter of handleConnection should be the WebSocket instance in use.

dreamy pecan
#

it's just type error

#

variable a can't have Socket

#

so, plz change type

merry shale
#

What type should I be using then?

#

Even if I just call a method that the client should normally have, like emit, it still throws an error.
It really seems like the issue is with the type of the client parameter.

wet light
merry shale
# wet light https://typescript-eslint.io/rules/no-unsafe-assignment/ Errors are hard aren't...

The documentation says you can’t assign a value of type any to another value.
In my screenshot, the handleConnection function is an implementation of OnGatewayConnection.

When I looked into the OnGatewayConnection interface, I found this code:

export interface OnGatewayConnection<T = any> {
  handleConnection(client: T, ...args: any[]): any;
}

Since client is defined as a generic any by default, I thought that if I moved the Socket type from handleConnection(client: Socket) up to the class level, it would solve the problem:

export class AppGateway implements OnGatewayConnection<Socket> {}

But even after passing the generic parameter, the issue still wasn’t resolved.
Or is there something I’ve misunderstood?

wet light