Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(common): strict type checking for ngtemplateoutlet #48374

Closed
wants to merge 1 commit into from

Conversation

tomalaforge
Copy link
Contributor

@tomalaforge tomalaforge commented Dec 6, 2022

When we create a context to inject inside our ngTemplateOutlet, the context was declare as Object, therefore, there are no compilation error.

Now if we add a context, we get error at compile type.

BREAKING CHANGE: If the 'ngTemplateOutletContext' is different from the context, it will result in a compile-time error.

Before the change, the following template was compiling:

interface MyContext {
  $implicit: string;
}

@component({
  standalone: true,
  imports: [NgTemplateOutlet],
  selector: 'person',
  template: `
    <ng-container
      *ngTemplateOutlet="
        myTemplateRef;
        context: { $implicit: 'test', xxx: 'xxx' }
      "></ng-container>
  `,
})
export class PersonComponent {
  myTemplateRef!: TemplateRef<MyContext>;
}

However, it does not compile now because the 'xxx' property does not exist in 'MyContext', resulting in the error: 'Type '{ $implicit: string; xxx: string; }' is not assignable to type 'MyContext'.'

The solution is either:

  • add the 'xxx' property to 'MyContext' with the correct type or
  • add '$any(...)' inside the template to make the error disappear. However, adding '$any(...)' does not correct the error but only preserves the previous behavior of the code.

fix #43510

@google-cla
Copy link

google-cla bot commented Dec 6, 2022

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@pullapprove pullapprove bot requested a review from dylhunn December 6, 2022 09:29
@tomalaforge tomalaforge force-pushed the feat-type-outlet branch 2 times, most recently from d1632be to 0dcffbc Compare December 6, 2022 09:44
@jessicajaniuk jessicajaniuk added the area: common Issues related to APIs in the @angular/common package label Dec 9, 2022
@ngbot ngbot bot added this to the Backlog milestone Dec 9, 2022
@tomalaforge
Copy link
Contributor Author

If someone can help me understand why the pipeline is failiing at the 'install java' step, I would be able to correct it. This will be greatly appreciated.
Thank you

@atscott
Copy link
Contributor

atscott commented Mar 14, 2023

Hi @tomalaforge - In general, I think this change looks correct and it's a great improvement.

That said, it's absolutely a breaking change since the type is more restrictive. There were several thousand build failures internally because of this (root causes are much lower since most failures are shared components).

At the very least, this will need the BREAKING CHANGE: ... footer to describe what's changed and what to do when there's a new failure. The easiest thing to do, and the approach I've landed on for the moment, is just adding $any to wrap the context in the template when there are failures. This obviously doesn't "fix" the type but it does make the failure go away without any behavioral changes to the code. You can probably describe this and some other possible solutions to common failures in the breaking change note and/or commit message.

@atscott atscott added breaking changes target: major This PR is targeted for the next major release cross-cutting: types labels Mar 14, 2023
@angular-robot angular-robot bot added the detected: breaking change PR contains a commit with a breaking change label Mar 15, 2023
@josephperrott josephperrott removed their request for review March 15, 2023 17:04
Copy link
Contributor

@atscott atscott left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quick update: There were only about a total of ~30 templates needing updates internally so this is certainly feasible to land. Adding my approval now and will mark for merge when internal code has been migrated.

reviewed-for: public-api, fw-common

@pullapprove pullapprove bot requested a review from atscott March 15, 2023 22:08
When we create a context to inject inside our ngTemplateOutlet, the context was declare as Object, therefore, there are no compilation error.

Now if we add a context, we get error at compile type.

BREAKING CHANGE:  If the 'ngTemplateOutletContext' is different from the context, it will result in a compile-time error.

Before the change, the following template was compiling:

```typescript
interface MyContext {
  $implicit: string;
}

@component({
  standalone: true,
  imports: [NgTemplateOutlet],
  selector: 'person',
  template: `
    <ng-container
      *ngTemplateOutlet="
        myTemplateRef;
        context: { $implicit: 'test', xxx: 'xxx' }
      "></ng-container>
  `,
})
export class PersonComponent {
  myTemplateRef!: TemplateRef<MyContext>;
}
```
However, it does not compile now because the 'xxx' property does not exist in 'MyContext', resulting in the error: 'Type '{ $implicit: string; xxx: string; }' is not assignable to type 'MyContext'.'

The solution is either:
- add the 'xxx' property to 'MyContext' with the correct type or
- add '$any(...)' inside the template to make the error disappear. However, adding '$any(...)' does not correct the error but only preserves the previous behavior of the code.

fix angular#43510
@atscott
Copy link
Contributor

atscott commented Mar 23, 2023

passing TGP

@atscott atscott added action: merge The PR is ready for merge by the caretaker merge: caretaker note Alert the caretaker performing the merge to check the PR for an out of normal action needed or note labels Mar 23, 2023
@atscott
Copy link
Contributor

atscott commented Mar 23, 2023

This PR was merged into the repository by commit d47fef7.

@atscott atscott closed this in d47fef7 Mar 23, 2023
@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 Apr 23, 2023
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: common Issues related to APIs in the @angular/common package breaking changes cross-cutting: types detected: breaking change PR contains a commit with a breaking change merge: caretaker note Alert the caretaker performing the merge to check the PR for an out of normal action needed or note target: major This PR is targeted for the next major release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ngTemplateOutletContext not checked strictly in template
5 participants