Title: Allow `inject()` usage as constructor parameter properties default · Issue #47606 · angular/angular · GitHub
Open Graph Title: Allow `inject()` usage as constructor parameter properties default · Issue #47606 · angular/angular
X Title: Allow `inject()` usage as constructor parameter properties default · Issue #47606 · angular/angular
Description: Which @angular/* package(s) are relevant/related to the feature request? compiler-cli, compiler Description Transformer in the way Injectables' dependencies are currently guessed by analyzing the constructor parameter types. This typing ...
Open Graph Description: Which @angular/* package(s) are relevant/related to the feature request? compiler-cli, compiler Description Transformer in the way Injectables' dependencies are currently guessed by analyzing the c...
X Description: Which @angular/* package(s) are relevant/related to the feature request? compiler-cli, compiler Description Transformer in the way Injectables' dependencies are currently guessed by analyzing t...
Opengraph URL: https://github.com/angular/angular/issues/47606
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Allow `inject()` usage as constructor parameter properties default","articleBody":"### Which @angular/* package(s) are relevant/related to the feature request?\n\ncompiler-cli, compiler\n\n### Description\n\n## Transformer in the way\r\n\r\nInjectables' dependencies are currently guessed by analyzing the constructor parameter types.\r\nThis typing information is lost after transforming TypeScript to ECMAScript. In order to fix this, the compiler has to run a transformer that converts this information into something like:\r\n```ts\r\nMyCmp.ctorParameters = () => [\r\n { type: MyService }\r\n];\r\n```\r\nCf. https://github.com/angular/angular/blob/3a60063a54d850c50ce962a8a39ce01cfee71398/packages/compiler-cli/src/transformers/downlevel_decorators_transform/downlevel_decorators_transform.ts\r\n\r\nThe problem with this approach is that it slows down community initiatives of integrating Angular with other tools. e.g. Vite, Vitest, SWC...\r\nThis forces tools to hack their way through. For instance, jest-preset-angular downloads the internal CLI transformer: https://github.com/thymikee/jest-preset-angular/blob/f24156ea1f7532c02c44d8d023b54623a4bccccd/scripts/prebuild.js#L9\r\n\r\n## `inject()` to the rescue\r\n\r\nThe good news is that an app using only `inject()` instead of constructor param types to retrieve dependencies will not need this transformer:\r\n```ts\r\nclass MyCmp {\r\n myService = inject(MyService);\r\n}\r\n```\r\n\r\nThe problem with using `inject()` function this way is that it doesn't allow injecting dependencies manually. This means that all tests will need the `TestBed` or similar to control dependencies resolution.\r\nTo allow manual instantiation, the current alternative would be something **non-explicit, verbose and error-prone** like this:\r\n\r\n```ts\r\nclass MyCmp {\r\n // let's not do this\r\n constructor(myService?: MyService) {\r\n this.myService = myService ?? inject(MyService);\r\n }\r\n}\r\n```\n\n### Proposed solution\n\nCompiler should bypass type-based dependency injection when a default value is set and the given type can't be resolved.\r\nThis would allow the following syntax which currently works if this transformer is disabled:\r\n\r\n```ts\r\nclass MyCmp {\r\n constructor(private myService = inject(MyService)) {}\r\n}\r\n```\r\n\r\nThis has the following advantages:\r\n- ✨ constructor parameters transformer is not needed anymore... so easier integration with existing tools\r\n- ✨ manual instantiation is still possible\r\n- ✨ all the benefits of `inject`: type-inference, consistent way of injecting class-based & injection-token based deps: `locale = inject(LOCALE_ID)`, controlling options (e.g. optional, self) through flags instead of decorators.\n\n### Alternatives considered\n\nUse `inject()` and never instantiate manually.","author":{"url":"https://github.com/yjaaidi","@type":"Person","name":"yjaaidi"},"datePublished":"2022-10-03T08:15:35Z","url":"https://github.com/angular/angular/issues/47606"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react-reconciler,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@d5750093facf |
| 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:0a22b3b8-73bd-304b-7c7c-5bb359e0ac13 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 96A8:334404:D6A271:11E49F3:6AED426D |
| html-safe-nonce | 84b3ae46348bb3df06b57fc65179f78dfbd325836c1dd6522f5bc053f4877731 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5NkE4OjMzNDQwNDpENkEyNzE6MTFFNDlGMzo2QUVENDI2RCIsInZpc2l0b3JfaWQiOiI0OTA1Mzk2MDIzMDIzNDUyNzgxIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 06f5162f0924964276deb1337c359a3c41a7dedecd2a65ac36d814f96f253ee4 |
| hovercard-subject-tag | issue:1394328946 |
| 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/47606/issue_layout |
| twitter:image | https://opengraph.githubassets.com/4faa5f30301254ecc6b8e0dcd9a97152ec3b4ef4c4d95a62e09ac11e0c1e917e/angular/angular/issues/47606 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/4faa5f30301254ecc6b8e0dcd9a97152ec3b4ef4c4d95a62e09ac11e0c1e917e/angular/angular/issues/47606 |
| og:image:alt | Which @angular/* package(s) are relevant/related to the feature request? compiler-cli, compiler Description Transformer in the way Injectables' dependencies are currently guessed by analyzing the c... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | yjaaidi |
| hostname | github.com |
| expected-hostname | github.com |
| None | 56fb42677ae87ed3e64410323d67980aa65d0485568a7858f874e7edfc6d1c11 |
| 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 | 9c9a37e2423ed382cf39f70b5aa4b4858b40a3b0 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width