admin管理员组文章数量:1397219
I have a scenario where in C#,
decimal? x; // number
decimal? y; // null
c=x*y //c returns null
where in typescript
let x:number=null;
let y:number=12;
c=x*y //c returns 0
**How to get null instead of 0 in typescript ?
I need to set generic way instead of using ternary operator and checking both properties null and returning null.
Is there any piler options I need to set in tsconfig.json ?**
I have a scenario where in C#,
decimal? x; // number
decimal? y; // null
c=x*y //c returns null
where in typescript
let x:number=null;
let y:number=12;
c=x*y //c returns 0
**How to get null instead of 0 in typescript ?
I need to set generic way instead of using ternary operator and checking both properties null and returning null.
Is there any piler options I need to set in tsconfig.json ?**
Share Improve this question edited Apr 13, 2018 at 23:01 Pac0 23.3k9 gold badges73 silver badges83 bronze badges asked Apr 13, 2018 at 22:28 garygary 1506 silver badges20 bronze badges 8- well, Typescript follows javascript / Ecmascript conventions. when doing math operations, some implicit conversions are done, and in particular null can be coerced to the number 0. – Pac0 Commented Apr 13, 2018 at 22:30
- is there any way to return null, because I need to save zero as zero and null as null in SQL – gary Commented Apr 13, 2018 at 22:31
-
null * number
should beNaN
, notnull
..? – Teemu Commented Apr 13, 2018 at 22:39 -
2
@gary Yep, but I said "it should be
NaN
." Accoding to any logic,null * number
is not a number, neither it isnull
..? That code snippet is a typical victim of the automatic type coercion JS does. – Teemu Commented Apr 13, 2018 at 22:50 - 3 @gary never use w3schools.. it is not a remotely credible reference. For your own sake you need to find something better – Aluan Haddad Commented Apr 13, 2018 at 23:16
1 Answer
Reset to default 7Why the result is zero in your case
Because in TypeScript/JS/ES, an arithmetic operation involving a number and null
will cause null
to be implicit converted to a number. And null gets converted to 0.
If you want the "C# null-swallowing" behavior, you indeed need to specify it explicitly :
let c = x === null || y === null ? null : x * y;
The 'natural' behavior in Typescript is the same as JavaScript / EcmaScript : if you have an operation with a number and null, null will be coerced to the number 0.
Is there any piler options I need to set in tsconfig.json ?**
To my knowledge, there isn't, and there shouldn't be, because that's how arithmetic operations work in ES / JS, and TS has no reason to try changing the arithmetic rules of ES towards the one used by C#.
I need to set generic way instead of using ternary operator and checking both p> properties null and returning null.
Another solution with NaN / undefined
To avoid this code cluttering and have a similar "some special values swallows every operation" like null
in C#, you could maybe use NaN
(Not a Number) instead of nulls.
This
would be easy to just transform every null into NaN.
any operation like x * y will give NaN if any operand is NaN, without any change to the code that performs putations.
If you really need nulls at the end then you can convert back NaNs to null. (be careful, to test for a number to be NaN, use
Math.IsNan()
, never try to pare with=== NaN
as this will always give false, evenNaN === NaN
)As a last but not least remark, if one of the operand is
undefined
, the result of an operation will also beNaN
. So actually, you could as well turn your nulls to undefined instead of NaNs, the conversion from C#null
to JS/TSundefined
might be more natural. Might be easier on the serialization side as well (hint :Newtonsoft.Json.NullValueHandling.Ignore
)
It is not sure this can be adapted to your situation, but it's worth checking if you want to avoid cluttering your code with conditionals like I wrote above.
版权声明:本文标题:javascript - Multiplying any number with null should return null instead of 0 in typescript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744107962a2591153.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论