admin管理员组文章数量:1379427
in TS 4.9.3 and angular 15 application, i have a service function isEmpty() with following snippet of code.
static isEmpty(value: any): boolean {
if (value === null || value === undefined || value === {} || value === []) {
return true;
}
}
and it generates This condition will always return 'false' since JavaScript pares objects by reference, not value.
issue for conditions value === {}
and value === []
.
in TS 4.9.3 and angular 15 application, i have a service function isEmpty() with following snippet of code.
static isEmpty(value: any): boolean {
if (value === null || value === undefined || value === {} || value === []) {
return true;
}
}
and it generates This condition will always return 'false' since JavaScript pares objects by reference, not value.
issue for conditions value === {}
and value === []
.
- 1 It means what it says: there's no point to the parison as it will always be false so your function always returns undefined. – jonrsharpe Commented Sep 20, 2023 at 7:06
1 Answer
Reset to default 5As the error states, in JavaScript object
s are pared by reference. So you cannot pare object values like {} === {}
. Something like this will always return false
because both {}
have different memory addresses.
In your case you pare an argument value
to a newly created object ({}
). The condition will always return false
since there is no way for the objects to have the same reference. TypeScript detects this and warns you about this by showing an piler error. Also the TypeScript control-flow analysis notices that the function never returns the annotated return type boolean
resulting in another piler error.
This feature was added in TypeScript 4.8 (Errors When Comparing Object and Array Literals)
[...] We believe that similar code in JavaScript is at best an early foot-gun for JavaScript developers, and at worst a bug in production code. That’s why TypeScript now disallows code like the following.
本文标签:
版权声明:本文标题:angular - <<closed>>This condition will always return 'false' since JavaScript compa 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744461649a2607267.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论