Skip to content

fix(compiler): narrow the type of the aliased if block expression#51952

Closed
crisbeto wants to merge 1 commit intoangular:mainfrom
crisbeto:if-block-alias-narrow-alternate
Closed

fix(compiler): narrow the type of the aliased if block expression#51952
crisbeto wants to merge 1 commit intoangular:mainfrom
crisbeto:if-block-alias-narrow-alternate

Conversation

@crisbeto
Copy link
Member

Currently the TCB for aliased if blocks looks something like this:

// Markup: `@if (expr; as alias) { {{alias}} }

if (block.condition) {
  var alias = block.condition;
  "" + alias;
}

The problem with this approach is that the type of alias won't be narrowed. This is something that NgIf currently supports.

These changes resolve the issue by emitting the variable outside the if block and using the variable reference instead:

// Markup: `@if (expr; as alias) { {{alias}} }

var alias = block.condition;
if (alias) {
  "" + alias;
}

@eneajaho
Copy link
Contributor

eneajaho commented Sep 29, 2023

What about this case? Where we alias the expr, but still use the expr and not the alias?

@if (expr; as alias) { 
  {{expr}} 
}

Asking because of this comment
CleanShot 2023-09-29 at 12 15 04@2x

Test in Ts playground

CleanShot 2023-09-29 at 12 21 44@2x
CleanShot 2023-09-29 at 12 21 55@2x

@crisbeto
Copy link
Member Author

That case isn't a problem since the generated code will always read the value off of the context:

image

@crisbeto crisbeto requested a review from dylhunn September 29, 2023 11:05
@crisbeto crisbeto added action: review The PR is still awaiting reviews from at least one requested reviewer target: major This PR is targeted for the next major release area: compiler Issues related to `ngc`, Angular's template compiler labels Sep 29, 2023
@ngbot ngbot bot added this to the Backlog milestone Sep 29, 2023
@crisbeto crisbeto marked this pull request as ready for review September 29, 2023 11:05
@eneajaho
Copy link
Contributor

eneajaho commented Sep 29, 2023

My question was more for the TCB?

CleanShot 2023-09-29 at 14 02 33@2x

It looks like the TCB will always create the alias and use that one inside if.

@if (expr; as alias) {
   {{expr}}
   {{alias}} 
}

would it generate this one?

 var _t1 = ((this).expr); 
 if (_t1) { 
   "" + ((this).expr);  // this.expr will be catched ? 
   "" + _t1;
 }

IMO, the correct one would be sth like:

 var _t1 = ((this).expr); 
 if (_t1 && ((this).expr)) { 
   "" + ((this).expr); 
   "" + _t1;
 }

@crisbeto
Copy link
Member Author

Yes it would, but it's not worth the additional effort to deduplicate it since this is only used during type checking so we don't care about the size of the code.

@crisbeto crisbeto modified the milestones: Backlog, v17-candidates Sep 30, 2023
Copy link
Contributor

@dylhunn dylhunn left a comment

Choose a reason for hiding this comment

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

Looks straightforward. Thanks for fixing this!

@dylhunn dylhunn removed the action: review The PR is still awaiting reviews from at least one requested reviewer label Oct 3, 2023
@crisbeto crisbeto added the action: merge The PR is ready for merge by the caretaker label Oct 3, 2023
Currently the TCB for aliased `if` blocks looks something like this:

```
// Markup: `@if (expr; as alias) { {{alias}} }

if (block.condition) {
  var alias = block.condition;
  "" + alias;
}
```

The problem with this approach is that the type of `alias` won't be narrowed. This is something that `NgIf` currently supports.

These changes resolve the issue by emitting the variable outside the `if` block and using the variable reference instead:

```
// Markup: `@if (expr; as alias) { {{alias}} }

var alias = block.condition;
if (alias) {
  "" + alias;
}
```
@crisbeto crisbeto force-pushed the if-block-alias-narrow-alternate branch from 33c36cc to 69ae684 Compare October 4, 2023 01:27
@alxhub
Copy link
Member

alxhub commented Oct 4, 2023

This PR was merged into the repository by commit 02edb43.

@alxhub alxhub closed this in 02edb43 Oct 4, 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 Nov 4, 2023
ChellappanRajan pushed a commit to ChellappanRajan/angular that referenced this pull request Jan 23, 2024
…gular#51952)

Currently the TCB for aliased `if` blocks looks something like this:

```
// Markup: `@if (expr; as alias) { {{alias}} }

if (block.condition) {
  var alias = block.condition;
  "" + alias;
}
```

The problem with this approach is that the type of `alias` won't be narrowed. This is something that `NgIf` currently supports.

These changes resolve the issue by emitting the variable outside the `if` block and using the variable reference instead:

```
// Markup: `@if (expr; as alias) { {{alias}} }

var alias = block.condition;
if (alias) {
  "" + alias;
}
```

PR Close angular#51952
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: compiler Issues related to `ngc`, Angular's template compiler target: major This PR is targeted for the next major release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

Comments