feat: add generic parameter for IEventPublisher in EventBus #1930
+252
−53
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
Currently, the
EventBus.publish
andEventBus.publishAll
methods always returnany
, whereas they could infer types from thepublish
andpublishAll
methods of theIEventPublisher
interface.What is the new behavior?
This PR introduces a new generic parameter for the
EventBus
which represents the type of the event publisher (extendingIEventPublisher
), allowing to infer the return types of thepublish
andpublishAll
methods.If the event publisher does not have a
publishAll
method—like in the case ofDefaultPubSub
—then the return type ofpublishAll
is an array of thepublish
results from the publisher. This clarifies the behavior of thepublishAll
method.By default,
publish
still returnsany
whilepublishAll
now returnsany[]
which better matches the returned array.Does this PR introduce a breaking change?
publishAll
now returnsany[]
instead ofany
by default. I think this is a "soft" breaking change.For this breaking change, here are the situations to consider:
publishAll
without using its result: no change.publishAll
:publishAll
method:publishAll
method:publishAll
: this cast will still compile but could be replaced by the inference allowed by the new generic parameter.Personally, I think this is a breaking change with not much impact.
Other information
I took the opportunity of this PR to add the
expect-type
library to more easily test types and generics. I selected this lib among many others (tsd
,ts-expect
,type-testing
,tstyche
) because this one is still maintained, used by many projects, and relatively comprehensive. If you have other suggestions, feel free to share them.I also added some missing generics tests on query handlers.