Visitar URL original
ngIf type narrowing feature does not work within "else" template · Issue #21504 · angular/angular · GitHub
The Wayback Machine - https://web.archive.org/web/20231016162951/https://github.com/angular/angular/issues/21504
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

ngIf type narrowing feature does not work within "else" template #21504

Open
noemi-salaun opened this issue Jan 12, 2018 · 6 comments
Open

ngIf type narrowing feature does not work within "else" template #21504

noemi-salaun opened this issue Jan 12, 2018 · 6 comments
Labels
area: compiler Issues related to `ngc`, Angular's template compiler compiler: template type-checking feature: under consideration Feature request for which voting has completed and the request is now under consideration feature: votes required Feature request which is currently still in the voting phase feature Issue that requests a new feature
Milestone

Comments

@noemi-salaun
Copy link

Relative to #20967

I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[x] Bug report  
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior

Compilation with option fullTemplateTypeCheck throw error Object is possibly 'undefined'

Expected behavior

No error, because the ngIf; else can work as a guard

Minimal reproduction of the problem with instructions

@Component({
  selector   : 'app-add-state',
  templateUrl: './add-state.component.html',
})
export class AddStateComponent {
  newState?: State
}
<div *ngIf="!newState; else createdTemplate">
  <!-- Some code to create a new state... -->
</div>
<ng-template #createdTemplate>
  State {{ newState.name }} created
</ng-template>
@vicb vicb added feature Issue that requests a new feature area: core Issues related to the framework runtime labels Jan 12, 2018
@vicb
Copy link
Contributor

vicb commented Jan 12, 2018 •

A workaround until this is implemented is

<ng-template #createdTemplate>
  State {{ $any(newState).name }} created
</ng-template>

@noemi-salaun
Copy link
Author

I think the simplest workaround would be

{{ newState!.name }}

To tell TypeScript that it is not null.

But I wasn't looking for a workaround, I simply wanted to warn the Angular team about this issue.

@ngbot ngbot bot added this to the Backlog milestone Jan 23, 2018
@pkozlowski-opensource pkozlowski-opensource added area: compiler Issues related to `ngc`, Angular's template compiler and removed area: core Issues related to the framework runtime labels Mar 17, 2020
@twittwer
Copy link

It seems to generally not working with ng-templates:

user?: { name: string }
<ng-content *ngIf="user; then currentUser else loginButton"><ng-content>
<ng-template #currentUser>{{ user.name }}</ng-template> <!-- error -->
<ng-template #loginButton>...</ng-template>

@angular-robot
Copy link
Contributor

angular-robot bot commented Jun 5, 2021

Just a heads up that we kicked off a community voting process for your feature request. There are 20 days until the voting process ends.

Find more details about Angular's feature request process in our documentation.

@angular-robot angular-robot bot added the feature: votes required Feature request which is currently still in the voting phase label Jun 5, 2021
@thw0rted
Copy link

thw0rted commented Jun 7, 2021

@vicb I would argue that this is a bug, not a feature -- and now that label apparently matters because it triggered the vote-bot to show up. (I think most users are going to wind up with strict templates by default sooner or later, so this is actually a pretty big deal.)

@angular-robot angular-robot bot added the feature: under consideration Feature request for which voting has completed and the request is now under consideration label Jun 8, 2021
@Ulrikop
Copy link

Ulrikop commented Apr 21, 2022

hm, pretty old issue for such a bug. is there any news about this?

I would appreciate not having to use $any so that I can get good feedback about bugs as early as possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: compiler Issues related to `ngc`, Angular's template compiler compiler: template type-checking feature: under consideration Feature request for which voting has completed and the request is now under consideration feature: votes required Feature request which is currently still in the voting phase feature Issue that requests a new feature
Projects
None yet
Development

No branches or pull requests

7 participants