Angular Ivy strictTemplates true 类型 'boolean |null' 不可分配给类型 'boolean' [英] Angular Ivy strictTemplates true Type 'boolean | null' is not assignable to type 'boolean'
问题描述
我已将我的应用更新到 9 版本.一切都很好,但我有问题将strictTemplates 设置为true.例如这段代码
I've updated my app to 9 version. Everything is alright but I've problem with strictTemplates set to true. For instance this code
loaded$: Observable<boolean>
[loaded]="loaded$ | async"
@Input() loaded!: boolean;
我收到错误类型 'boolean |null' 不能分配给类型 'boolean'.
I've got the error Type 'boolean | null' is not assignable to type 'boolean'.
修复错误
@Input() loaded!: boolean | null;
但是我看不到有人acn解释我的意思,好吗?
But I cant see the point someone acn explain me, please?
推荐答案
原因是因为 async
管道返回签名类似于
总是,因为它在等待来自异步调用的响应时向模板返回 null.
The reason is because the async
pipes return signature is something like <T>(input$: Observable<T>): T | null
always, because it returns null to the template while it's awaiting a response from an asynchronous call.
更多相关信息:https://angular.io/guide/模板类型检查#strict-null-checks
你可以做你做过的事情并允许空值,或者如果你知道它永远不会为空,使用非空断言运算符:
You can do what you've done and allow null, or if you know it will never be null, use a non null assertion operator:
[loaded]="(loaded$ | async)!"
或在此处禁用类型检查:
or disable type checking here:
[loaded]="$any(loaded$ | async)"
或者对于这种特殊情况,您可能可以这样做:
or for this particular case you could probably do something like this:
[loaded]="(loaded$ | async) || false"
这篇关于Angular Ivy strictTemplates true 类型 'boolean |null' 不可分配给类型 'boolean'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!