You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Angular templates don't play well with strictNullChecks #19756
[ ] 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
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
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"/>?
Activity
kemsky commentedon Oct 17, 2017
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:
Why can't we have something like
<input ([hero])="hero?.name" placeholder="name"/>?angular-automatic-lock-bot commentedon Sep 12, 2019
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.