Skip to content

feat(compiler): allow ngIf to use the ngIf expression directly as a guard - #20972

Merged
alxhub merged 1 commit into
angular:masterfrom
chuckjaz:i20967
Dec 18, 2017
Merged

alxhub merged 1 commit into
angular:masterfrom
chuckjaz:i20967

Conversation

@chuckjaz

Copy link
Copy Markdown
Contributor

Allows a directive to use the expression passed directly to a property
as a guard instead of filtering the type through a type guard function.

This more accurately matches the intent of the ngIf usage of its template
enabling better type inference.

Moved NgIf to using this type of guard instead of a function guard.

Closes: #20967

PR Type

What kind of change does this PR introduce?

[x] Feature

What is the current behavior?

#20967

Using the type guard mechanism introduced in e7d9cb3 the following expression is not narrowed as expected.

  <div *ngIf="!!person">
    {{person.name}
  </div>

because the type guard function only infers that the expression !!person is true but does not further infer that implies person is not undefined causing person.name to report that person might be undefined.

What is the new behavior?

The code generated for an *ngIf uses the guard expression directly as the template guard instead of using a type guard function. This allows TypeScript to infer the correct types for person in the above example.

Does this PR introduce a breaking change?

[ ] Yes
[x] No

@mary-poppins

Copy link
Copy Markdown

You can preview bd6bacc at https://pr20972-bd6bacc.ngbuilds.io/.

…uard

Allows a directive to use the expression passed directly to a property
as a guard instead of filtering the type through a type expression.

This more accurately matches the intent of the ngIf usage of its template
enabling better type inference.

Moved NgIf to using this type of guard instead of a function guard.

Closes: angular#20967
@mary-poppins

Copy link
Copy Markdown

You can preview c97ce64 at https://pr20972-c97ce64.ngbuilds.io/.

@jasonaden jasonaden added the area: core Issues related to the framework runtime label Dec 13, 2017
@chuckjaz chuckjaz added action: merge The PR is ready for merge by the caretaker target: major This PR is targeted for the next major release labels Dec 13, 2017
@alxhub
alxhub requested a review from mhevery December 13, 2017 21:37
@alxhub alxhub added action: review The PR is still awaiting reviews from at least one requested reviewer and removed action: merge The PR is ready for merge by the caretaker labels Dec 13, 2017
@chuckjaz chuckjaz 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 Dec 15, 2017
@alxhub
alxhub merged commit 82bcd83 into angular:master Dec 18, 2017
@alxhub

alxhub commented Dec 18, 2017

Copy link
Copy Markdown
Member

PullApprove broken, Misko approved.

@angular-automatic-lock-bot

Copy link
Copy Markdown

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 Sep 13, 2019
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: core Issues related to the framework runtime cla: yes target: major This PR is targeted for the next major release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ngIf type narrowing feature introduced by #20702 is incomplete

7 participants