René's URL Explorer Experiment


Title: forwardRef breaks at runtime with ES2015 · Issue #30106 · angular/angular · GitHub

Open Graph Title: forwardRef breaks at runtime with ES2015 · Issue #30106 · angular/angular

X Title: forwardRef breaks at runtime with ES2015 · Issue #30106 · angular/angular

Description: 🐞 bug report Affected Package The issue is caused by package @angular/core Is this a regression? No. Description Using forwardRef as described in https://angular.io/api/core/forwardRef while targeting ES2015 results on a Uncaught Referen...

Open Graph Description: 🐞 bug report Affected Package The issue is caused by package @angular/core Is this a regression? No. Description Using forwardRef as described in https://angular.io/api/core/forwardRef while target...

X Description: 🐞 bug report Affected Package The issue is caused by package @angular/core Is this a regression? No. Description Using forwardRef as described in https://angular.io/api/core/forwardRef while target...

Opengraph URL: https://github.com/angular/angular/issues/30106

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"forwardRef breaks at runtime with ES2015","articleBody":"\u003c!--🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅\r\n\r\nOh hi there! 😄\r\n\r\nTo expedite issue processing please search open and closed issues before submitting a new one.\r\nExisting issues often contain information about workarounds, resolution, or progress updates.\r\n\r\n🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅--\u003e\r\n\r\n\r\n# 🐞 bug report\r\n\r\n### Affected Package\r\n\u003c!-- Can you pin-point one or more @angular/* packages as the source of the bug? --\u003e\r\n\u003c!-- ✍️edit: --\u003e The issue is caused by package @angular/core\r\n\r\n\r\n### Is this a regression?\r\n\r\n\u003c!-- Did this behavior use to work in the previous version? --\u003e\r\n\u003c!-- ✍️--\u003e No.\r\n\r\n\r\n### Description\r\n\r\nUsing `forwardRef` as described in https://angular.io/api/core/forwardRef while targeting ES2015 results on a `Uncaught ReferenceError: Cannot access 'Lock' before initialization` runtime error.\r\n\r\n\r\n## 🔬 Minimal Reproduction\r\n- make a new project `ng new forward-ref-project \u0026\u0026 cd forward-ref-project`\r\n- ensure `tsconfig.json` contains `\"target\": \"es2015\",`\r\n- replace the contents of `src/main.ts` with:\r\n```\r\nimport { Inject, forwardRef, ReflectiveInjector } from '@angular/core';\r\nclass Door {\r\n  lock: Lock;\r\n\r\n  // Door attempts to inject Lock, despite it not being defined yet.\r\n  // forwardRef makes this possible.\r\n  constructor(@Inject(forwardRef(() =\u003e Lock)) lock: Lock) { this.lock = lock; }\r\n}\r\n\r\n// Only at this point Lock is defined.\r\nclass Lock { }\r\n\r\nconst injector = ReflectiveInjector.resolveAndCreate([Door, Lock]);\r\nconst door = injector.get(Door);\r\nconsole.log(door instanceof Door);\r\nconsole.log(door.lock instanceof Lock);\r\n```\r\n- `ng serve -o`\r\n\r\n## 🔥 Exception or Error\r\n```\r\nUncaught ReferenceError: Cannot access 'Lock' before initialization\r\n    at Module../src/main.ts (main.ts:21)\r\n    at __webpack_require__ (bootstrap:78)\r\n    at Object.2 (main.ts:30)\r\n    at __webpack_require__ (bootstrap:78)\r\n    at checkDeferredModules (bootstrap:45)\r\n    at Array.webpackJsonpCallback [as push] (bootstrap:32)\r\n    at main.js:1\r\n```\r\n\r\n\r\n## 🌍  Your Environment\r\n\r\n**Angular Version:**\r\n\u003cpre\u003e\u003ccode\u003e\r\nAngular CLI: 8.0.0-beta.18\r\nNode: 10.10.0\r\nOS: win32 x64\r\nAngular: 8.0.0-beta.14\r\n... animations, common, compiler, compiler-cli, core, forms\r\n... language-service, platform-browser, platform-browser-dynamic\r\n... router\r\n\r\nPackage                           Version\r\n-----------------------------------------------------------\r\n@angular-devkit/architect         0.800.0-beta.18\r\n@angular-devkit/build-angular     0.800.0-beta.18\r\n@angular-devkit/build-optimizer   0.800.0-beta.18\r\n@angular-devkit/build-webpack     0.800.0-beta.18\r\n@angular-devkit/core              8.0.0-beta.18\r\n@angular-devkit/schematics        8.0.0-beta.18\r\n@angular/cli                      8.0.0-beta.18\r\n@ngtools/webpack                  8.0.0-beta.18\r\n@schematics/angular               8.0.0-beta.18\r\n@schematics/update                0.800.0-beta.18\r\nrxjs                              6.4.0\r\ntypescript                        3.4.5\r\nwebpack                           4.30.0\r\n\u003c/code\u003e\u003c/pre\u003e\r\n\r\n**Anything else relevant?**\r\n\r\n`forwardRef` is provided specifically for the purpose of referencing something that isn't defined. This is useful in breaking [circular dependencies](https://angular.io/guide/dependency-injection-in-action#break-circularities-with-a-forward-class-reference-forwardref), and when declaring both services and components in the [same file](https://angular.io/guide/dependency-injection#create-and-register-an-injectable-service).\r\n\r\n`forwardRef` works because it delays the resolution of the reference to a time at which it is already declared through the callback indirection. In the [API example](https://angular.io/api/core/forwardRef), the symbol we want to delay resolution is `Lock`:\r\n```\r\nclass Door {\r\n  lock: Lock;\r\n\r\n  // Door attempts to inject Lock, despite it not being defined yet.\r\n  // forwardRef makes this possible.\r\n  constructor(@Inject(forwardRef(() =\u003e Lock)) lock: Lock) { this.lock = lock; }\r\n}\r\n\r\n// Only at this point Lock is defined.\r\nclass Lock { }\r\n```\r\n\r\nBut `Lock` is actually being referenced in more places than just inside `forwardRef`. It is also being used as a TS type in the class property, and in the constructor parameter.\r\n\r\nTypes don't usually have a runtime representation so that shouldn't be a problem. But constructor types are an exception and actually do have a runtime representation. We can see this by looking at the transpiled code:\r\n\r\n```\r\nimport * as tslib_1 from \"tslib\";\r\nimport { Inject, forwardRef, ReflectiveInjector } from '@angular/core';\r\nlet Door = class Door {\r\n    // Door attempts to inject Lock, despite it not being defined yet.\r\n    // forwardRef makes this possible.\r\n    constructor(lock) { this.lock = lock; }\r\n};\r\nDoor = tslib_1.__decorate([\r\n    tslib_1.__param(0, Inject(forwardRef(() =\u003e Lock))),\r\n    tslib_1.__metadata(\"design:paramtypes\", [Lock])\r\n], Door);\r\n// Only at this point Lock is defined.\r\nclass Lock {\r\n}\r\nconst injector = ReflectiveInjector.resolveAndCreate([Door, Lock]);\r\nconst door = injector.get(Door);\r\nconsole.log(door instanceof Door);\r\nconsole.log(door.lock instanceof Lock);\r\n```\r\n\r\nThe `Lock` type in the for the constructor parameter was transpiled into `tslib_1.__metadata(\"design:paramtypes\", [Lock])`. This reference does not have a delayed resolution like the injected `forwardRef` and is instead immediately resolved, resulting in `Uncaught ReferenceError: Cannot access 'Lock' before initialization`.\r\n\r\nThis error isn't observed when targetting ES5 however. We can understand why by looking at the code when transpiled to ES5 :\r\n```\r\nimport * as tslib_1 from \"tslib\";\r\nimport { Inject, forwardRef, ReflectiveInjector } from '@angular/core';\r\nvar Door = /** @class */ (function () {\r\n    // Door attempts to inject Lock, despite it not being defined yet.\r\n    // forwardRef makes this possible.\r\n    function Door(lock) {\r\n        this.lock = lock;\r\n    }\r\n    Door = tslib_1.__decorate([\r\n        tslib_1.__param(0, Inject(forwardRef(function () { return Lock; }))),\r\n        tslib_1.__metadata(\"design:paramtypes\", [Lock])\r\n    ], Door);\r\n    return Door;\r\n}());\r\n// Only at this point Lock is defined.\r\nvar Lock = /** @class */ (function () {\r\n    function Lock() {\r\n    }\r\n    return Lock;\r\n}());\r\nvar injector = ReflectiveInjector.resolveAndCreate([Door, Lock]);\r\nvar door = injector.get(Door);\r\nconsole.log(door instanceof Door);\r\nconsole.log(door.lock instanceof Lock);\r\n```\r\n\r\nIn ES5 there are no `class` declarations, so TS instead uses a `var`. One important different between `var` and `class/let/const` is that the latter are all subject to the [Temporal Dead Zone](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let#Temporal_dead_zone). \r\n\r\nIn practical terms the TDZ means that using a `var` before it is declared resolves to `undefined`, but using a `class/let/const` instead throws a `ReferenceError`. This is the error we are seeing here.\r\n\r\nA possible workaround is to **not** declare the type in the constructor:\r\n```\r\n// Instead of adding the type in the parameter\r\nconstructor(@Inject(forwardRef(() =\u003e Lock)) lock: Lock) { \r\n  this.lock = lock;\r\n}\r\n\r\n// Add it as a cast in the constructor body\r\nconstructor(@Inject(forwardRef(() =\u003e Lock)) lock) {\r\n  this.lock = lock as Lock;\r\n}\r\n```\r\n\r\nThis will change the transpiled code and remove the reference, avoiding the `ReferenceError`:\r\n```\r\nDoor = tslib_1.__decorate([\r\n    tslib_1.__param(0, Inject(forwardRef(() =\u003e Lock))),\r\n    tslib_1.__metadata(\"design:paramtypes\", [Object])\r\n                                             ^^^^^^ was Lock before\r\n], Door);\r\n```\r\n\r\nOne important note is that the `ReferenceError` does not come up on Angular CLI projects compiled with AOT. This is because there we actually transform transpiled TS code and remove Angular decorators, so the metadata reference (`tslib_1.__metadata(\"design:paramtypes\", [Lock])`) never reaches the browser and thus there is no `ReferenceError`.\r\n","author":{"url":"https://github.com/filipesilva","@type":"Person","name":"filipesilva"},"datePublished":"2019-04-25T08:56:10.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":19},"url":"https://github.com/30106/angular/issues/30106"}

route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:0c7c250c-53e1-80e0-3d7c-70a9df91d5e7
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idCF8C:2345DC:875515:C474E6:6A54ACBD
html-safe-nonce4d1033f45174988f7a5c038ddf7967c31d6767806d8b6cd6f22f96a3d328bb57
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDRjhDOjIzNDVEQzo4NzU1MTU6QzQ3NEU2OjZBNTRBQ0JEIiwidmlzaXRvcl9pZCI6IjE4NjI2NTM2NDY0ODUxMDU4NTMiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmacb311dd87dbe4fa736020b03a2b5d4074bd1810e284e67cb90c208c34cd0708d4
hovercard-subject-tagissue:437082804
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/angular/angular/30106/issue_layout
twitter:imagehttps://opengraph.githubassets.com/bfc55adc43891442de378c24585c3d530b45ed59b21cbb4f0354083f9e5ae00d/angular/angular/issues/30106
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/bfc55adc43891442de378c24585c3d530b45ed59b21cbb4f0354083f9e5ae00d/angular/angular/issues/30106
og:image:alt🐞 bug report Affected Package The issue is caused by package @angular/core Is this a regression? No. Description Using forwardRef as described in https://angular.io/api/core/forwardRef while target...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamefilipesilva
hostnamegithub.com
expected-hostnamegithub.com
Noneb9a586c06a05a7a86fc7e3f4dbd03e42f6869085879aa184aa6369456dbd50fb
turbo-cache-controlno-preview
go-importgithub.com/angular/angular git https://github.com/angular/angular.git
octolytics-dimension-user_id139426
octolytics-dimension-user_loginangular
octolytics-dimension-repository_id24195339
octolytics-dimension-repository_nwoangular/angular
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id24195339
octolytics-dimension-repository_network_root_nwoangular/angular
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
releasef8cfff6384f62259baea531c7f58bc7040f8dd92
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/angular/angular/issues/30106#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fangular%2Fangular%2Fissues%2F30106
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub Copilot appDirect agents from issue to mergehttps://github.com/features/ai/github-app
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
View all resourceshttps://github.com/resources
GitHub SponsorsFund open source developershttps://github.com/open-source/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/open-source/accelerator
GitHub Starshttps://stars.github.com
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fangular%2Fangular%2Fissues%2F30106
Sign up https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=angular%2Fangular
Reloadhttps://github.com/angular/angular/issues/30106
Reloadhttps://github.com/angular/angular/issues/30106
Reloadhttps://github.com/angular/angular/issues/30106
Please reload this pagehttps://github.com/angular/angular/issues/30106
angular https://github.com/angular
angularhttps://github.com/angular/angular
Notifications https://github.com/login?return_to=%2Fangular%2Fangular
Fork 27.5k https://github.com/login?return_to=%2Fangular%2Fangular
Star 101k https://github.com/login?return_to=%2Fangular%2Fangular
Code https://github.com/angular/angular
Issues 974 https://github.com/angular/angular/issues
Pull requests 154 https://github.com/angular/angular/pulls
Discussions https://github.com/angular/angular/discussions
Actions https://github.com/angular/angular/actions
Projects https://github.com/angular/angular/projects
Security and quality 24 https://github.com/angular/angular/security
Insights https://github.com/angular/angular/pulse
Code https://github.com/angular/angular
Issues https://github.com/angular/angular/issues
Pull requests https://github.com/angular/angular/pulls
Discussions https://github.com/angular/angular/discussions
Actions https://github.com/angular/angular/actions
Projects https://github.com/angular/angular/projects
Security and quality https://github.com/angular/angular/security
Insights https://github.com/angular/angular/pulse
forwardRef breaks at runtime with ES2015https://github.com/angular/angular/issues/30106#top
https://github.com/devversion
area: compilerIssues related to `ngc`, Angular's template compilerhttps://github.com/angular/angular/issues?q=state%3Aopen%20label%3A%22area%3A%20compiler%22
freq1: lowhttps://github.com/angular/angular/issues?q=state%3Aopen%20label%3A%22freq1%3A%20low%22
type: bug/fixhttps://github.com/angular/angular/issues?q=state%3Aopen%20label%3A%22type%3A%20bug%2Ffix%22
Backloghttps://github.com/angular/angular/milestone/82
https://github.com/filipesilva
filipesilvahttps://github.com/filipesilva
on Apr 25, 2019https://github.com/angular/angular/issues/30106#issue-437082804
https://angular.io/api/core/forwardRefhttps://angular.io/api/core/forwardRef
circular dependencieshttps://angular.io/guide/dependency-injection-in-action#break-circularities-with-a-forward-class-reference-forwardref
same filehttps://angular.io/guide/dependency-injection#create-and-register-an-injectable-service
API examplehttps://angular.io/api/core/forwardRef
Temporal Dead Zonehttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let#Temporal_dead_zone
devversionhttps://github.com/devversion
area: compilerIssues related to `ngc`, Angular's template compilerhttps://github.com/angular/angular/issues?q=state%3Aopen%20label%3A%22area%3A%20compiler%22
freq1: lowhttps://github.com/angular/angular/issues?q=state%3Aopen%20label%3A%22freq1%3A%20low%22
type: bug/fixhttps://github.com/angular/angular/issues?q=state%3Aopen%20label%3A%22type%3A%20bug%2Ffix%22
BacklogNo due datehttps://github.com/angular/angular/milestone/82
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.