Visitar URL original
fix(common): narrow `NgIf` context variables in template type checker by ranma42 · Pull Request #36627 · angular/angular · GitHub
The Wayback Machine - https://web.archive.org/web/20220321124213/https://github.com/angular/angular/pull/36627
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): narrow NgIf context variables in template type checker #36627

Closed
wants to merge 1 commit into from

Conversation

ranma42
Copy link
Contributor

@ranma42 ranma42 commented Apr 14, 2020

When the NgIf directive is used in a template, its context variables
can be used to capture the bound value. This is sometimes used in
complex expressions, where the resulting value is captured in a
context variable. There's two syntax forms available:

  1. Binding to NgIfContext.ngIf using the as syntax:
<span *ngIf="enabled && user as u">{{u.name}}</span>
  1. Binding to NgIfContext.$implicit using the let syntax:
<span *ngIf="enabled && user; let u">{{u.name}}</span>

Because of the semantics of ngIf, it is known that the captured
context variable is truthy, however the template type checker
would not consider them as such and still report errors when
strict is enabled.

This commit updates NgIf's context guard to make the types of the
context variables truthy, avoiding the issue.

Based on #35125

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • angular.io application / infrastructure changes
  • Other... Please describe:

What is the current behavior?

When compiling in strict mode, the compiler rejects

<span *ngIf="enabled && user as u">{{u.name}}</span>

What is the new behavior?

The typing of ngIf is more accurate, excluding most falsy values (the main exception should be NaNs) from the type passed to the template body.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

@pullapprove pullapprove bot requested review from alxhub and IgorMinar Apr 14, 2020
@JoostK
Copy link
Member

@JoostK JoostK commented Apr 14, 2020

Hey @ranma42,

I didn't consider this case when opening #35125 as my understanding was that NonNullable was sufficient, but your example shows otherwise.

Could you please rebase so that CI can run?

@JoostK
Copy link
Member

@JoostK JoostK commented Apr 14, 2020

Additionally, please change the commit scope from "ivy" to "common", as the "ivy" scope is no longer used (and will fail CI checks)

@ranma42 ranma42 force-pushed the fix/false-ngif branch 2 times, most recently from c49d7bf to b1b29c6 Compare Apr 14, 2020
@ranma42
Copy link
Contributor Author

@ranma42 ranma42 commented Apr 14, 2020

@JoostK thank you for your guidance :)
After following your suggestions I found out that some tests were failing because of the indentation in the goldens, but AFAICT they now look good.

@ranma42 ranma42 changed the title fix(ivy): narrow NgIf context variables in template type checker fix(common): narrow NgIf context variables in template type checker Apr 16, 2020
@ranma42
Copy link
Contributor Author

@ranma42 ranma42 commented Apr 21, 2020

I believe I have fixed the commit message and rebased as requested.
If this is this still blocked on my side, please ping me ;)

alxhub
alxhub approved these changes Apr 28, 2020
@ranma42
Copy link
Contributor Author

@ranma42 ranma42 commented May 7, 2020

Are the "failing" checks an issue in the changes?
Or are they just blocked on the review?

@petebacondarwin
Copy link
Member

@petebacondarwin petebacondarwin commented May 7, 2020

The latter.

@ranma42
Copy link
Contributor Author

@ranma42 ranma42 commented Jun 4, 2020

Any news? Do I need to rebase this on a more recent master?

@petebacondarwin
Copy link
Member

@petebacondarwin petebacondarwin commented Jun 4, 2020

@ranma42 - please do rebase on master - I will ping @IgorMinar to do the public API review.

When the `NgIf` directive is used in a template, its context variables
can be used to capture the bound value. This is sometimes used in
complex expressions, where the resulting value is captured in a
context variable. There's two syntax forms available:

1. Binding to `NgIfContext.ngIf` using the `as` syntax:
```html
<span *ngIf="enabled && user as u">{{u.name}}</span>
```

2. Binding to `NgIfContext.$implicit` using the `let` syntax:
```html
<span *ngIf="enabled && user; let u">{{u.name}}</span>
```

Because of the semantics of `ngIf`, it is known that the captured
context variable is truthy, however the template type checker
would not consider them as such and still report errors when
`strict` is enabled.

This commit updates `NgIf`'s context guard to make the types of the
context variables truthy, avoiding the issue.

Based on angular#35125
@alxhub
Copy link
Contributor

@alxhub alxhub commented Jun 4, 2020

Copy link
Member

@jelbourn jelbourn left a comment

LGTM

Reviewed-for: public-api

@ranma42
Copy link
Contributor Author

@ranma42 ranma42 commented Jun 25, 2020

Is there anything I should do to make the PR process proceed?

@jelbourn
Copy link
Member

@jelbourn jelbourn commented Jun 26, 2020

This is just awaiting other reviewers

Copy link
Member

@petebacondarwin petebacondarwin left a comment

Should we include NaN in the excluded list too?

@JoostK
Copy link
Member

@JoostK JoostK commented Jul 14, 2020

Should we include NaN in the excluded list too?

That's not a type AFAIK?

@JoostK
Copy link
Member

@JoostK JoostK commented Jul 14, 2020

Indeed, NaN is only a value, and using typeof NaN would be the number type.

@petebacondarwin
Copy link
Member

@petebacondarwin petebacondarwin commented Jul 14, 2020

Gosh!

@pullapprove pullapprove bot requested a review from petebacondarwin Jul 16, 2020
@ranma42
Copy link
Contributor Author

@ranma42 ranma42 commented Jul 22, 2020

Literal NaN is not supported (yet?) in typescript; it is being discussed/tracked in microsoft/TypeScript#28682 but if possible I would not block this PR on that.

While the new typing is not yet exact, it is as narrow as possible with the current types supported by TS.

@pullapprove pullapprove bot removed the request for review from IgorMinar Jul 23, 2020
Copy link
Member

@petebacondarwin petebacondarwin left a comment

Reviewed-for: public-api

@pullapprove pullapprove bot requested review from AndrewKushnir and atscott Jul 28, 2020
Copy link
Contributor

@atscott atscott left a comment

Reviewed-for: public-api

@atscott
Copy link
Contributor

@atscott atscott commented Jul 28, 2020

Splaktar added a commit to angular-hispano/angular that referenced this issue Aug 8, 2020
…angular#36627)

When the `NgIf` directive is used in a template, its context variables
can be used to capture the bound value. This is sometimes used in
complex expressions, where the resulting value is captured in a
context variable. There's two syntax forms available:

1. Binding to `NgIfContext.ngIf` using the `as` syntax:
```html
<span *ngIf="enabled && user as u">{{u.name}}</span>
```

2. Binding to `NgIfContext.$implicit` using the `let` syntax:
```html
<span *ngIf="enabled && user; let u">{{u.name}}</span>
```

Because of the semantics of `ngIf`, it is known that the captured
context variable is truthy, however the template type checker
would not consider them as such and still report errors when
`strict` is enabled.

This commit updates `NgIf`'s context guard to make the types of the
context variables truthy, avoiding the issue.

Based on angular#35125

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

@angular-automatic-lock-bot angular-automatic-lock-bot bot commented Aug 29, 2020

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 29, 2020
profanis added a commit to profanis/angular that referenced this issue Sep 5, 2020
…angular#36627)

When the `NgIf` directive is used in a template, its context variables
can be used to capture the bound value. This is sometimes used in
complex expressions, where the resulting value is captured in a
context variable. There's two syntax forms available:

1. Binding to `NgIfContext.ngIf` using the `as` syntax:
```html
<span *ngIf="enabled && user as u">{{u.name}}</span>
```

2. Binding to `NgIfContext.$implicit` using the `let` syntax:
```html
<span *ngIf="enabled && user; let u">{{u.name}}</span>
```

Because of the semantics of `ngIf`, it is known that the captured
context variable is truthy, however the template type checker
would not consider them as such and still report errors when
`strict` is enabled.

This commit updates `NgIf`'s context guard to make the types of the
context variables truthy, avoiding the issue.

Based on angular#35125

PR Close angular#36627
@ranma42 ranma42 deleted the fix/false-ngif branch Sep 11, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

7 participants