Skip to content

Conversation

@crisbeto
Copy link
Member

@crisbeto crisbeto commented Jul 25, 2024

Includes the following fixes that came up as a result of running the inject migration against Material and the CDK.

fix(migrations): add alias to inject migration

Adds a shorter alias to the inject migration.

fix(migrations): remove generic arguments from the injected type reference

Currently if an injected type has type arguments, we copy it over together with the type arguments to inject, because inject() isn't able to infer the generic properly otherwise. E.g. if there's constructor(el: ElementRef<HTMLElement>) we produce inject<ElementRef<HTMLElement>>(ElementRef<HTMLElement>);.

These changes drop the generics from the inject() parameter since we're overwriting the type anyway. The example from above would become inject<ElementRef<HTMLElement>>(ElementRef);.

fix(migrations): account for parameters with union types

This came up in Material where we had a constructor(@Optional() foo: Foo | null) which ended up producing incorrect code, because the union type was preserved.

These changes resolve the issue by picking out the first non-literal type from the union for the inject call.

fix(migrations): unwrap injected forwardRef

Updates the inject migration to unwrap the forwardRef call in cases like constructor(@Inject(forwardRef(() => Foo)) foo: Foo);, because the forwardRef will type the initializer to any and it shouldn't be necessary.

crisbeto added 4 commits July 25, 2024 11:14
Adds a shorter alias to the inject migration.
…rence

Currently if an injected type has type arguments, we copy it over together with the type arguments to inject, because `inject()` isn't able to infer the generic properly otherwise. E.g. if there's `constructor(el: ElementRef<HTMLElement>)` we produce `inject<ElementRef<HTMLElement>>(ElementRef<HTMLElement>);`.

These changes drop the generics from the `inject()` parameter since we're overwriting the type anyway. The example from above would become `inject<ElementRef<HTMLElement>>(ElementRef);`.
This can up in Material where we had a `constructor(@optionA() foo: Foo | null)` which ended up producing incorrect code, because the union type was preserved.

These changes resolve the issue by picking out the first non-literal type from the union for the `inject` call.
Updates the inject migration to unwrap the `forwardRef` call in cases like `constructor(@Inject(forwardRef(() => Foo)) foo: Foo);`, because the `forwardRef` will type the initializer to `any` and it shouldn't be necessary.
@crisbeto crisbeto added action: review The PR is still awaiting reviews from at least one requested reviewer area: migrations Issues related to `ng update`/`ng generate` migrations target: minor This PR is targeted for the next minor release labels Jul 25, 2024
@crisbeto crisbeto requested a review from devversion July 25, 2024 10:08
@ngbot ngbot bot modified the milestone: Backlog Jul 25, 2024
@crisbeto crisbeto added action: merge The PR is ready for merge by the caretaker and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels Jul 25, 2024
@dylhunn
Copy link
Contributor

dylhunn commented Jul 26, 2024

This PR was merged into the repository by commit aae9646.

The changes were merged into the following branches: main

@dylhunn dylhunn closed this in 166166d Jul 26, 2024
dylhunn pushed a commit that referenced this pull request Jul 26, 2024
…rence (#57127)

Currently if an injected type has type arguments, we copy it over together with the type arguments to inject, because `inject()` isn't able to infer the generic properly otherwise. E.g. if there's `constructor(el: ElementRef<HTMLElement>)` we produce `inject<ElementRef<HTMLElement>>(ElementRef<HTMLElement>);`.

These changes drop the generics from the `inject()` parameter since we're overwriting the type anyway. The example from above would become `inject<ElementRef<HTMLElement>>(ElementRef);`.

PR Close #57127
dylhunn pushed a commit that referenced this pull request Jul 26, 2024
This can up in Material where we had a `constructor(@optionA() foo: Foo | null)` which ended up producing incorrect code, because the union type was preserved.

These changes resolve the issue by picking out the first non-literal type from the union for the `inject` call.

PR Close #57127
dylhunn pushed a commit that referenced this pull request Jul 26, 2024
Updates the inject migration to unwrap the `forwardRef` call in cases like `constructor(@Inject(forwardRef(() => Foo)) foo: Foo);`, because the `forwardRef` will type the initializer to `any` and it shouldn't be necessary.

PR Close #57127
vladboisa pushed a commit to vladboisa/angular that referenced this pull request Jul 29, 2024
Adds a shorter alias to the inject migration.

PR Close angular#57127
vladboisa pushed a commit to vladboisa/angular that referenced this pull request Jul 29, 2024
…rence (angular#57127)

Currently if an injected type has type arguments, we copy it over together with the type arguments to inject, because `inject()` isn't able to infer the generic properly otherwise. E.g. if there's `constructor(el: ElementRef<HTMLElement>)` we produce `inject<ElementRef<HTMLElement>>(ElementRef<HTMLElement>);`.

These changes drop the generics from the `inject()` parameter since we're overwriting the type anyway. The example from above would become `inject<ElementRef<HTMLElement>>(ElementRef);`.

PR Close angular#57127
vladboisa pushed a commit to vladboisa/angular that referenced this pull request Jul 29, 2024
This can up in Material where we had a `constructor(@optionA() foo: Foo | null)` which ended up producing incorrect code, because the union type was preserved.

These changes resolve the issue by picking out the first non-literal type from the union for the `inject` call.

PR Close angular#57127
vladboisa pushed a commit to vladboisa/angular that referenced this pull request Jul 29, 2024
Updates the inject migration to unwrap the `forwardRef` call in cases like `constructor(@Inject(forwardRef(() => Foo)) foo: Foo);`, because the `forwardRef` will type the initializer to `any` and it shouldn't be necessary.

PR Close angular#57127
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Aug 26, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

action: merge The PR is ready for merge by the caretaker area: migrations Issues related to `ng update`/`ng generate` migrations target: minor This PR is targeted for the next minor release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants