How to make an alias for a class that has a lot of generics (typescript)?

38 Views Asked by At

I am utilizing the socket.io library for typescript. Whenever referencing a Socket or Server type in the code, four generic parameters are expected. See the docs for a full example, but the relevant part is this:

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

And similarly with sockets.

My question is, how do I alias this long type? And, is it even possible? I have tried the following:

  • type keyword:
export type DWFServer = Server<ClientToServerEvents, ServerToClientEvents, InterServerEvents, SocketData>
  • interface keyword:
export interface DWFServer extends Server<ClientToServerEvents, ServerToClientEvents, InterServerEvents, SocketData> {}
  • class keyword:
export class DWFServer extends Server< ClientToServerEvents, ServerToClientEvents, InterServerEvents, SocketData > {}

In the case of the type and interface keyword, I get the following compile error:

TS2693: 'DWFServer' only refers to a type, but is being used as a value here.

In the case of the class keyword, I got no compilation failure, but a runtime error instantiating the aliased class:

TypeError: Class constructor Server cannot be invoked without 'new'

Its also worth noting that the type alias is in another package, and I am directly exporting these types in that package's index.ts file.

Does anyone know how I can make this alias work?

0

There are 0 best solutions below