send functions should have generic types
See original GitHub issueIs your feature request related to a problem? Please describe.
Currently functions like emit are not typable:
socket.emit('my-event-name', dto);
This applies to both socket.io server and client.
Describe the solution you’d like I would like to have generic types on all emit functions to (optionally) type the send-object and the response-object:
socket.emit<void, MyDtoInterface>('my-event-name', dto);
socket.emit<MyResponseDtoInterface, MyDtoInterface>('my-event-name', dto, responseDto => {
// ...
});
Now if an interface changes TypeScript throws an error if the objects are not updated.
Suggested solution Functions could look something like this:
// Server:
emit<TInput = any>(ev: string, data: TInput);
// Client:
emit<TResult = any, TInput = any>(ev: string, data: TInput, (response: TResult) => {});
Issue Analytics
- State:
- Created 3 years ago
- Comments:9 (6 by maintainers)
Top Results From Across the Web
Documentation - Generics - TypeScript
Once we've written the generic identity function, we can call it in one of two ways. The first way is to pass all...
Read more >How To Use Generics in TypeScript - DigitalOcean
Generics can appear in functions, types, classes, and interfaces. ... This way the calling code does not need to pass any type parameters....
Read more >Passing a generic type to a function in typescript
So I will be able to pass the type as a generic so if there's like 5 or even more different types I...
Read more >Generic Methods - Java™ Tutorials
Generic methods allow type parameters to be used to express dependencies among the types of one or more arguments to a method and/or...
Read more >Creating generic functions | Learn TypeScript
We can't use the above function because it is restricted for arrays of strings. Wouldn't it be nice if we could pass the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I have a WIP implementation that would fix this issue on this branch. @darrachequesne would there be interest in potentially merging something like that? If so, I can complete the implementation.
The implementation adds two optional type parameters to
Socket
,Server
andNamespace
. These type parameter are a mapping of event names to listener types; by default, it’s a mapping of allstring
s to(...args: any[]) => void
, so it retains the current behavior if no type parameters are passed. However, users can optionally pass a mapping of their event names to the types of the listeners that listen to those events.For example Angular http client does it like this:
https://github.com/angular/angular/blob/2c757591ee334ced115bc93ba1b944803bb0c07e/packages/common/http/src/client.ts#L2291
Or all NestJS Microservice Clients: https://github.com/nestjs/nest/blob/eb239abd8261b3328945865790ffc3c377b60827/packages/microservices/client/client-proxy.ts#L60
I specifically used NestJS as the source of my proposal above.
It is easy to implement two generics, it does not add any extra code and is totally optional to use.