m4ss1m0g / mediatr-ts

Mediator patter for Typescript, inspired to MediatR csharp library
MIT License
46 stars 9 forks source link

Ability for mediatr.send() to automatically know the return type? #25

Closed shaunek-hero closed 2 months ago

shaunek-hero commented 3 months ago

I've used the .NET MediatR package and enjoyed it so I have stumbled upon your package while working on a TS project and I like what you have done, kuddos.

One thing I've admired about the .NET package was that when you call var result = await mediatr.Send(myCommand) the IDE (or compiler I guess?) is able to smartly know the proper return type. As in the handler method's return type is typically something like Task<int> and then magically the var result is inferred automatically as an int. This package's api surface is so very similar to the original .NET package, but I notice that with this package when we do var result = await mediator.send(myCommand) the result is an unknown... I do understand that specifying await mediator.send<number>(myCommand); is a work around and the IDE thinks result is now a number, but it sure would be amazing if the send method could somehow automatically infer the return type of the handler method.

I'm guessing that this might be a limitation of how Typescript+Javascript work (???), since I've seen that the @nestjs/cqrs package has a similar limitation where it's QueryBus/CommandBus.execute() will return an any.

And to be clear, I'm not yet a user of this package, I have just created a very small demo with your package and still trying to decide if I want to use it. Still I'm interested in it. Maybe you have already determined this isn't a possibility.

m4ss1m0g commented 2 months ago

I'm guessing that this might be a limitation of how Typescript+Javascript work "TypeScript is JavaScript with syntax for types" (cit.), it is on top of javascript which is an untyped language.

With that said I think that what you want must be searched on the Typescript framework, and from what I know the TS doesn't have this kind of feature.

shaunek-hero commented 2 months ago

Yeah you might be right about that. I'm not entirely certain though - I've started using some new-ish libraries that do some insane Typscript kung fu. I know Typscript okay as an app developer, but the guys who developed stuff like Drizzle and ElysiaJS are next level, and after using their libs (and small amounts of peeking under the hood) I wonder if what I'm asking for is possible.

In any case no reason to keep this ticket open since frankly I'm not even actively using this library and the idea was just something I thought I would float. All the best to you.