Skip to content

Angular templates don't play well with strictNullChecks #19756

Closed

Description

@Casebash

I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[ ] Bug report  
[x] 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:
Currently if Typescript has strictNullChecks=true then it is very hard to work with variables that are nullable.

For example, if I have: hero?: Hero; inside my HeroDetailComponent class from the Heroes tutorial, then

<ng-container *ngIf="hero; else no_hero">
		<h2>{{hero.name}} details!</h2>
		<input ([hero])="hero.name" placeholder="name"/>
</ng-container>

Then there will be an error for hero.name as it might be null and the binding won't work.

Expected / new behavior:

The template engine should be able to determine that the variable hero is not null within the if statement and so both of the components above should work. Alternatively, it could add syntax to allow you to explicitly cast a variable to a more specific type that would pass this information further into the template.

Minimal reproduction of the problem with instructions:

Open up the Heroes demo and change hero to a nullable variable and add strictNullChecks into tsconfig.json

AngularJS version: 4.4.4

Browser: Chrome 61

Anything else:
Current workaround:

<ng-container *ngIf="hero; else no_hero; let HERO">
		<h2>{{HERO.name}} details!</h2>
		<input [ngModel]="HERO.name" (ngModelChange)="HERO.name=$event" placeholder="name"/>
	</ng-container>

Activity

  1. kemsky commented on Oct 17, 2017

    @kemsky

    You can use ? i.e. <h2>{{HERO?.name}} details!</h2>, but unfortunately it will not work for writable properties, which is very annoying when you deal with forms.

    Perfectly valid C# example:

                List<int> ints = null;
                ints?.Add(1);

    Why can't we have something like <input ([hero])="hero?.name" placeholder="name"/>?

  2. added
    area: coreIssues related to the framework runtime
    featureLabel used to distinguish feature request from other issues
    on Oct 17, 2017
  3. angular-automatic-lock-bot commented on Sep 12, 2019

    @angular-automatic-lock-bot

    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.

  4. locked and limited conversation to collaborators on Sep 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: coreIssues related to the framework runtimefeatureLabel used to distinguish feature request from other issues

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions