Title: EventEmitter.emit() should only accept T, not T | undefined. · Issue #35119 · angular/angular · GitHub
Open Graph Title: EventEmitter.emit() should only accept T, not T | undefined. · Issue #35119 · angular/angular
X Title: EventEmitter.emit() should only accept T, not T | undefined. · Issue #35119 · angular/angular
Description: 🐞 bug report Affected Package @angular/core Is this a regression? No Description The signature of EventEmiter.emit was modelled according to the signature of rxjs' Subject.next (see #10058 ). But the signature of Subject.next has a desig...
Open Graph Description: 🐞 bug report Affected Package @angular/core Is this a regression? No Description The signature of EventEmiter.emit was modelled according to the signature of rxjs' Subject.next (see #10058 ). But t...
X Description: 🐞 bug report Affected Package @angular/core Is this a regression? No Description The signature of EventEmiter.emit was modelled according to the signature of rxjs' Subject.next (see #10058 ). B...
Opengraph URL: https://github.com/angular/angular/issues/35119
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"EventEmitter.emit() should only accept T, not T | undefined.","articleBody":"# 🐞 bug report\r\n\r\n### Affected Package\r\n\r\n@angular/core\r\n\r\n\r\n### Is this a regression?\r\n\r\nNo\r\n\r\n\r\n### Description\r\n\r\nThe signature of `EventEmiter.emit` was modelled according to the signature of rxjs' `Subject.next` (see #10058 ). But the signature of `Subject.next` has a [design flaw](https://github.com/ReactiveX/rxjs/issues/2852) which has been copied to `EventEmitter.emit`.\r\n\r\nThis issue is essentially the same as https://github.com/ReactiveX/rxjs/issues/2852 , but applied to angular's EventEmitter. See repro below for details.\r\n\r\n\r\n## 🔬 Minimal Reproduction\r\n\r\n```ts\r\nconst em = new EventEmitter\u003cstring>();\r\nem.asObservable().subscribe(s => {\r\n s.substring(0, 1); // boom\r\n});\r\nem.emit();\r\n```\r\n\r\n### Expected behavior\r\n\r\nA compile error at line 5\r\n\r\n### Actual behavior\r\n\r\nA runtime error at line 3\r\n\r\n### Details\r\n\r\nThe signature of `emit` allows emitting `undefined` on an event emitter which declares a non-nullable type.\r\nThis defeats the whole purpose of typescript's --strictNullCheck flag.\r\n\r\n\r\n## 🔥 Exception or Error\r\n\r\nNo error, that's the problem.\r\n\r\n\r\n## 🌍 Your Environment\r\n\r\nAll versions since #10058\r\n\r\n## 🔧 Suggested fix\r\n\r\nThe suggested fix is simple enough, but it is a breaking change:\r\n- Make the parameter of `emit` non-optional. One can still write `em.emit()` provided the EventEmitter is of type `EventEmitter\u003cvoid>`.\r\n- Make the default type of T `void` instead of `any` (see [this](https://github.com/ReactiveX/rxjs/issues/5066#issuecomment-581462164)) for details.\r\n\r\n```diff\r\ndiff --git a/packages/core/src/event_emitter.ts b/packages/core/src/event_emitter.ts\r\nindex 07143d9774..eaf84b7ad0 100644\r\n--- a/packages/core/src/event_emitter.ts\r\n+++ b/packages/core/src/event_emitter.ts\r\n@@ -61,7 +61,7 @@ import {Subject, Subscription} from 'rxjs';\r\n * @see [Observables in Angular](guide/observables-in-angular)\r\n * @publicApi\r\n */\r\n-export class EventEmitter\u003cT extends any> extends Subject\u003cT> {\r\n+export class EventEmitter\u003cT extends any = void> extends Subject\u003cT> {\r\n /**\r\n * @internal\r\n */\r\n@@ -83,7 +83,7 @@ export class EventEmitter\u003cT extends any> extends Subject\u003cT> {\r\n * Emits an event containing a given value.\r\n * @param value The value to emit.\r\n */\r\n- emit(value?: T) { super.next(value); }\r\n+ emit(value: T) { super.next(value); }\r\n \r\n /**\r\n * Registers handlers for events emitted by this instance.\r\n```","author":{"url":"https://github.com/hmil","@type":"Person","name":"hmil"},"datePublished":"2020-02-03T15:37:31Z","url":"https://github.com/angular/angular/issues/35119"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:7e688911-4f64-5e82-e5e6-16d2ca2f3f0c |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E4D8:A71F9:122DF2C:16DF256:6B015747 |
| html-safe-nonce | 2637d5250e009afbe3d3fb6a6bfd136f5d7edc2f97cd3184352128c017241c17 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFNEQ4OkE3MUY5OjEyMkRGMkM6MTZERjI1Njo2QjAxNTc0NyIsInZpc2l0b3JfaWQiOiI0Njg0NzQ5ODk1MDMzMjQ3NTU5IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | e17383243049d5815ef0a9b604a88bc0d140a75be4733147130d62a807243b88 |
| hovercard-subject-tag | issue:559161710 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/angular/angular/35119/issue_layout |
| twitter:image | https://opengraph.githubassets.com/bd5a6e6ca288372cf05da9505f178df060153a4650398c2782e210fc0572f3e3/angular/angular/issues/35119 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/bd5a6e6ca288372cf05da9505f178df060153a4650398c2782e210fc0572f3e3/angular/angular/issues/35119 |
| og:image:alt | 🐞 bug report Affected Package @angular/core Is this a regression? No Description The signature of EventEmiter.emit was modelled according to the signature of rxjs' Subject.next (see #10058 ). But t... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | hmil |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| go-import | github.com/angular/angular git https://github.com/angular/angular.git |
| octolytics-dimension-user_id | 139426 |
| octolytics-dimension-user_login | angular |
| octolytics-dimension-repository_id | 24195339 |
| octolytics-dimension-repository_nwo | angular/angular |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 24195339 |
| octolytics-dimension-repository_network_root_nwo | angular/angular |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width