Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: typed response command #1863

Closed
wants to merge 1 commit into from

Conversation

Farenheith
Copy link

@Farenheith Farenheith commented Dec 4, 2024

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

What is the current behavior?

The execute methods of CommandBus and QueryBus return any by default or the return type informed as generics, there's no way to intrinsically associate a return type to a command.

Issue Number: N/A

What is the new behavior?

If the command/query class attends two requisites the return type will be inferred:

  • It extends of ICommand<ReturnType>/IQuery<ReturnType>;
  • It is merged with an interface declaration of the same name that inherits ICommand<ReturnType>/IQuery<ReturnType>

Example:

class MyCommand implements ICommand<string> {}
interface MyCommand extends ICommand<string> {}

If the command doesn't attend both requisites, commandBus, and queryBus will still return any, making of this change a safe one, as no previous behavior got broken.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

I'll update whatever documentation I need if the idea is accepted.

Although it's not ideal to merge a class and interface declaration, in some specific scenarios, like this one, this may bring some benefits and more type-safe code. I hope someday Typescript evolves so that this can work without the merge trick.

Having commands that are merged with interface
declarations will allow to have strong type
inference for commandBus and queryBus response.
Although is not perfect, it's a little improvement
for the ones interested in, and maybe in the future
Typescript may have support it without the merge trick
@kamilmysliwiec
Copy link
Member

Thanks for your contribution! This feature is already implemented in this PR #1861

@Farenheith
Copy link
Author

Farenheith commented Dec 4, 2024

Thank you for your reply. You made the base class in a better way than I proposed, I think it could avoid the need for the type merge my approach needed.

However, it only affected the Handlers, not the command and query buses. I think with a new overload for the execute method in those classes the result of execute can also be strongly typed, which will result in more safe-type operations with the buses!

Can I create a PR for the PR's branch proposing this change?

edit: never mind. It is there too, sorry.

@Farenheith Farenheith deleted the typed-response-command branch December 4, 2024 10:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants