Skip to content

bug(core): EventEmitter#subscribe() should be properly typed #29016

Open
@jorroll

Description

@jorroll

🚀 feature request

Relevant Package

This feature request is for @angular/core

Description

Currently, subscribing to an EventEmitter (via EventEmitter#subscribe()) stripes the EventEmitter's type information and converts it to any. This is undesirable, as already pointed out in #6934.

That issue was closed with a nondescript explanation that ~ "you shouldn't use the EventEmitter's subscribe() method". However, EventEmitter#subscribe() is not marked as internal API.

The suggestion that someone should create a duplicate property, using an rxjs subject, solely for the purpose of improved typing, is unsatisfactory.

This is a request to do whatever needs to be done to allow subscribing to EventEmitter directly while preserving type information.

Describe the solution you'd like

The type information, which is already present on EventEmitter, should be retained.

Describe alternatives you've considered

Currently, the suggested workaround seems to be to create a duplicate property and update both the @Output() property and the duplicate Subject property whenever the associated data changes.

e.g.

class MyComponent {
  @Input() name: string;
  
  @Output()
  nameChange = new EventEmitter<string>();
  nameChange2 = new Subject<string>();

  @Input() weather: {prediction: string};
  
  @Output()
  weatherChange = new EventEmitter<{prediction: string}>();
  weatherChange2 = new Subject<{prediction: string}>();

  private updateName(name: string) {
    this.name = name;
    this.nameChange.emit(name);
    this.nameChange2.next(name);
  }

  private weatherName(value: {prediction: string}) {
    this.weather = value;
    this.weatherChange.emit(value)
    this.weatherChange2.next(value)
  }
}
``

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions