question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

send functions should have generic types

See original GitHub issue

Is 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:closed
  • Created 3 years ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
MaximeKjaercommented, Feb 26, 2021

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 and Namespace. These type parameter are a mapping of event names to listener types; by default, it’s a mapping of all strings 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.

1reaction
MickLcommented, Mar 11, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found