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 be NaN, not null..? – 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 is null ..? 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
 |  Show 3 more ments

1 Answer 1

Reset to default 7

Why 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

  1. would be easy to just transform every null into NaN.

  2. any operation like x * y will give NaN if any operand is NaN, without any change to the code that performs putations.

  3. 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, even NaN === NaN)

  4. As a last but not least remark, if one of the operand is undefined, the result of an operation will also be NaN. So actually, you could as well turn your nulls to undefined instead of NaNs, the conversion from C# null to JS/TS undefined 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.

本文标签: javascriptMultiplying any number with null should return null instead of 0 in typescriptStack Overflow