IssueRaiser

Allow `inject()` usage as constructor parameter properties default

### 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 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: ```ts MyCmp.ctorParameters = () => [ { type: MyService } ]; ``` Cf. https://github.com/angular/angular/blob/3a60063a54d850c50ce962a8a39ce01cfee71398/packages/compiler-cli/src/transformers/downlevel_decorators_transform/downlevel_decorators_transform.ts The problem with this approach is that it slows down community initiatives of integrating Angular with other tools. e.g. Vite, Vitest, SWC... This 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 ## `inject()` to the rescue The good news is that an app using only `inject()` instead of constructor param types to retrieve dependencies will not need this transformer: ```ts class MyCmp { myService = inject(MyService); } ``` The 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 resolutio

Posted by yjaaidi on Oct 3, 2022, 8:15 AM

Tags

Related issues

Raise real-world problems, find people facing the same issue, and discuss solutions.2,500 issues0 comments

reliability (SaaS)

Allow `inject()` usage as constructor parameter properties default

yjaaidiOct 3, 2022, 8:15 AM

### 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 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: ```ts MyCmp.ctorParameters = () => [ { type: MyService } ]; ``` Cf. https://github.com/angular/angular/blob/3a60063a54d850c50ce962a8a39ce01cfee71398/packages/compiler-cli/src/transformers/downlevel_decorators_transform/downlevel_decorators_transform.ts The problem with this approach is that it slows down community initiatives of integrating Angular with other tools. e.g. Vite, Vitest, SWC... This 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 ## `inject()` to the rescue The good news is that an app using only `inject()` instead of constructor param types to retrieve dependencies will not need this transformer: ```ts class MyCmp { myService = inject(MyService); } ``` The 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 resolutio

Comments (0)

No comments yet. Start the discussion.

Comment on this issue