Decide how to reconcile `async` pipe with strict template type checking
See original GitHub issue🚀 feature request
Relevant Package
This feature request is for @angular/common
Description
Currently the async
pipe is difficult to use with strictTemplates
and strictNullChecks
enabled. This is because the async
pipe emits null
if the underlying observable has not emitted yet. Therefore using the async
pipe with an input that does not accept null
will result in a compilation error.
@devversion has done a more detailed write-up of this issue. I’m including a summary of the possible solutions here, but please refer to the write-up for more details.
Describe the solution you’d like
The ideal solution would be to make async
pipe not emit until the underlying observable emits, however this would likely be a fairly breaking change. It would probably need to be done in a phased manner, while introducing something like asyncEager
that maintains the old behavior to make migration easier.
Describe alternatives you’ve considered
-
An option that would split the responsibility for fixing between framework and libraries is to introduce something like
@RequiredInput(fallbackValue)
that automatically replacesnull
withfallbackValue
. Libraries could then update their inputs to use@RequiredInput
whennull
is not expected. -
Another option that would place the responsibility on libraries is to ask them to just handle
null
and allow it to be passed in the template even if its not part of the input’s official type (usingstatic ngAcceptInputType_*
) -
Finally, a third alternative is to just pass the responsibility down to the app developer to fill in a default:
[someInput]="(value$ | async) || defaultValue"
Issue Analytics
- State:
- Created 4 years ago
- Reactions:8
- Comments:8 (6 by maintainers)
we discussed this today, and we agreed that to fix this properly, we should look at typing of inputs in general and properly allow inputs to be optional, required, etc.
for v9 the best we can likely do is that for users that want to use
strictTemplates
andstrictNullChecks
together, they can opt out of strict null typecheck specifically for input bindings viastrictNullInputTypes
: https://v9.angular.io/guide/template-typecheck#troubleshooting-template-errorsThis should be explicitly documented in the v9 updating guide or template type checking guide.
I consider this resolved. With the way Angular works today, at runtime the
async
pipe will set anull
value into inputs. I think thatstrictTemplates
should be enforcing that this is possible, unless you explicitly opt out (strictNullInputTypes: false
).