Skip to content

fix(migrations): preserve any type of untyped EventEmitter in output migration - #72348

Open
odolbeau wants to merge 1 commit into
angular:mainfrom
odolbeau:fix/output-migration-untyped-event-emitter
Open

odolbeau wants to merge 1 commit into
angular:mainfrom
odolbeau:fix/output-migration-untyped-event-emitter

Conversation

@odolbeau

@odolbeau odolbeau commented Dec 4, 2026

Copy link
Copy Markdown

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
  • Documentation content changes
  • angular.dev application / infrastructure changes
  • Other... Please describe:

What is the current behavior?

@Output() saved = new EventEmitter(); is migrated to readonly saved = output();. The original emitter emits any, the migrated one emits void, so a parent binding like (saved)="onSaved($event)" with onSaved(value: string) no longer compiles under strictTemplates.

Issue Number: N/A

What is the new behavior?

Untyped emitters are migrated to output<any>(), which preserves the original type.

Outputs emitted without a value (this.saved.emit(), (click)="saved.next()", host bindings, across compilation units) still migrate to output(): OutputEmitterRef.emit(value: T) requires an argument, so output<any>() would break those calls.

To detect argument-less calls in templates, the shared template reference visitor now includes the root expression in readAstPath (it skipped it because ASTWithSource forwards visit to its inner AST).

Does this PR introduce a breaking change?

  • Yes
  • No

@angular-robot angular-robot Bot added the area: migrations Issues related to `ng update`/`ng generate` migrations label Dec 4, 2026
@pullapprove
pullapprove Bot requested a review from kirjs December 4, 2026 13:28
@ngbot ngbot Bot added this to the Backlog milestone Dec 4, 2026
@JeanMeche
JeanMeche requested review from JeanMeche and removed request for kirjs December 4, 2026 17:14

@JeanMeche JeanMeche left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for tackling this! I have one design question and a few implementation issues to address:

Design question

Defaulting all untyped new EventEmitter() declarations to output<any>() whenever no 0-arg emit() is seen has a couple of significant downsides:

  • Many projects enable @typescript-eslint/no-explicit-any, so turning untyped new EventEmitter() into output<any>() will introduce lint errors even for void/unused outputs (as seen in the 9 updated specs).
  • If an untyped output is emitted without arguments (comp.someChange.emit()) in a consumer/test/subclass outside the analyzed compilation units, migrating it to output<any>() will break those callers (TS2554: Expected 1 arguments, but got 0).

Have you considered inverting the heuristic—keeping output() by default and only using output<any>() when an emit(value) / next(value) with an argument is observed?

Implementation issues

  1. Mixed emit() and emit(value) calls break compilation:
    If an untyped emitter is called both without and with a value:

    @Output() saved = new EventEmitter();
    foo() {
      this.saved.emit();
      this.saved.emit('hello');
    }

    outputsEmittedWithoutValue[declId] becomes true, so it migrates to output() (OutputEmitterRef<void>), which breaks this.saved.emit('hello') (TS2345) and any parent (saved)="onSaved($event)" handler. In this mixed case, the output should either be marked as a problematicUsage (skipped) or handled with output<any>() + a // TODO comment on the empty emit.

  2. Safe navigation someChange?.emit() is missed in templates:
    checkNonTsReferenceEmitsWithoutValue checks call instanceof SafeCall, which handles someChange.emit?.(), but checkNonTsReferenceAccessesField only checks parentRead instanceof PropertyRead. Since SafePropertyRead extends ASTWithName (not PropertyRead), (click)="someChange?.emit()" returns null and won't be detected.

  3. Duplicate Binary entry in readAstPath:
    The fix in template_reference_visitor.ts (this.visit(expressionNode instanceof ASTWithSource ? expressionNode.ast : expressionNode, [])) is a great catch, but visitBinary in that same file manually passes [...context, ast] to this.visit(ast.left, ...) / this.visit(ast.right, ...), which now duplicates ast in readAstPath. Please pass context there instead.

  4. Test coverage:
    Please add dedicated tests for:

    • template and host binding emit() without value vs. with value,
    • safe navigation (someChange?.emit() / someChange.emit?.()),
    • mixed emit() + emit(value) calls,
    • cross-file / multi-unit empty emit().

(Also note: this PR will conflict with #72347 in output-migration.ts, output-replacements.ts, and output-migration.spec.ts depending on merge order.)

@odolbeau
odolbeau force-pushed the fix/output-migration-untyped-event-emitter branch from 0a22551 to 23300ad Compare December 5, 2026 08:30
@odolbeau

odolbeau commented Dec 5, 2026

Copy link
Copy Markdown
Author

Thanks for the thorough review! All points addressed:

  • Design: inverted the heuristic — untyped emitters now migrate to output() by default, and to output<any>() only when an emit(value)/next(value) is observed (TS, templates, host bindings, across compilation units).
  • Mixed calls: migrated to output<any>() with a // TODO: The 'emit' function requires a mandatory any argument above the empty TS calls, consistent with typed emitters. The TODOs are only applied once the global decision is known, and deduplicated for files shared by multiple compilation units.
  • Safe navigation: checkNonTsReferenceAccessesField now accepts SafePropertyRead (this also fixes .next → .emit for someChange?.next()).
  • visitBinary: now passes context.
  • Tests: added dedicated tests for TS/template/host emits with and without value, safe navigation (?.emit() / .emit?.()), mixed calls, cross-file, and multi-unit (combine).

Note: as with typed emitters, empty emit() calls in templates don't get a TODO in the mixed case.

…migration

An untyped `new EventEmitter()` emits `any`, but the output migration turned it
into `output()`, which emits `void`. Parent bindings such as
`(saved)="onSaved($event)"` then fail to compile under `strictTemplates`.

The migration now generates `output<any>()` for untyped emitters that are
emitted with a value (`emit(value)`/`next(value)` in TypeScript, templates or
host bindings). Other untyped emitters keep migrating to `output()`. When an
untyped emitter is emitted both with and without a value, a TODO comment is
added above the calls without a value, as `OutputEmitterRef<any>.emit` requires
an argument.

Emits using safe navigation (`someChange?.emit()`) in templates and host
bindings are now detected as well.
@odolbeau
odolbeau force-pushed the fix/output-migration-untyped-event-emitter branch from 23300ad to 5b1df36 Compare December 5, 2026 10:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: migrations Issues related to `ng update`/`ng generate` migrations

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants