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(microservices): add event typings to the @EventPattern decorator #14798

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

joelday
Copy link

@joelday joelday commented Mar 18, 2025

Adds overloads for @EventPattern to allow validation that decorated method signatures match event payload types based on a specified mapping type. Includes tests demonstrating correct usage and error cases with // @ts-expect-error.

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?

Issue Number: N/A

What is the new behavior?

Adds overloads to the @EventPattern decorator that creates a static type constraint between the pattern and the decorated method's parameter type.

Example:

type FooMessage = {
  foo: string;
};

type BarMessage = {
  bar: number;
};

type EventTypes = {
  'event-pattern-foo': FooMessage;
  'event-pattern-bar': BarMessage;
};

class FooBarTestComponent {
  // correct
  @EventPattern<EventTypes>('event-pattern-bar')
  testBar(event: BarMessage) {}

  // incorrect (foo is specified, but the parameter type is `string` instead of `FooMessage`
  @EventPattern<EventTypes>('event-pattern-foo')
  testFoo(event: string) {}
}

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

  • I'm unsure of what documentation should be added/where that lives.
  • I'm unsure of whether or not topic is an appropriate name for these overloads instead of metadata.

Adds overloads for `@EventPattern` to allow validation that decorated method signatures match event payload types based on a specified mapping type. Includes tests demonstrating correct usage and error cases with `// @ts-expect-error`.
@coveralls
Copy link

Pull Request Test Coverage Report for Build 17672a4a-de8d-4a97-8fa2-abef07e8be32

Details

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 89.292%

Totals Coverage Status
Change from base Build 4f1bfe2a-3a8e-4431-8410-1516cf911760: 0.0%
Covered Lines: 7146
Relevant Lines: 8003

💛 - Coveralls

@micalevisk micalevisk requested a review from Copilot March 27, 2025 15:52

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds static type overloads for the @EventPattern decorator to ensure the decorated method's parameter types align with the event payload types specified in a mapping type, along with tests to verify the desired behavior.

  • Introduces generic, typed overloads for @EventPattern in the decorator file
  • Adds type safety tests in the corresponding spec file to demonstrate correct usage and expected error conditions

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
packages/microservices/test/decorators/event-pattern.decorator.spec.ts Adds new static type safety tests for the @EventPattern decorator overloads
packages/microservices/decorators/event-pattern.decorator.ts Introduces typed overloads for the EventPattern decorator to enforce correct event payload types
Comments suppressed due to low confidence (1)

packages/microservices/decorators/event-pattern.decorator.ts:33

  • [nitpick] Consider aligning the parameter naming between the typed overloads (using 'topic') and the untyped overloads (using 'metadata') for improved consistency and clarity. This may help reduce confusion about the intended usage.
<TEventTypes extends Record<string, any>>(
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