简体   繁体   English

将任何数字与 null 相乘应返回 null 而不是打字稿中的 0

[英]Multiplying any number with null should return null instead of 0 in typescript

I have a scenario where in C#,我有一个场景,在 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 ? **如何在打字稿中获得 null 而不是 0 ?

I need to set generic way instead of using ternary operator and checking both properties null and returning null.我需要设置通用方式而不是使用三元运算符并检查两个属性为 null 并返回 null。

Is there any compiler options I need to set in tsconfig.json ?**我需要在 tsconfig.json 中设置任何编译器选项吗?**

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.因为在 TypeScript/JS/ES 中,涉及数字和null的算术运算会导致null隐式转换为数字。 And null gets converted to 0.并且 null 被转换为 0。

If you want the "C# null-swallowing" behavior, you indeed need to specify it explicitly :如果您想要“C# null-swallowing”行为,您确实需要明确指定它:

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. Typescript 中的“自然”行为与 JavaScript / EcmaScript 相同:如果您有一个带有数字和 null 的操作,则 null 将被强制为数字 0。

Is there any compiler options I need to set in tsconfig.json ?**我需要在 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#.据我所知,没有,也不应该有,因为这就是 ES / JS 中算术运算的工作方式,并且 TS 没有理由尝试将 ES 的算术规则更改为 C# 使用的算术规则。


I need to set generic way instead of using ternary operator and checking both p> properties null and returning null.我需要设置通用方式而不是使用三元运算符并检查 p> 属性为 null 并返回 null。

Another solution with NaN / undefined 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.为了避免此代码混乱并在 C# 中具有类似的“某些特殊值会吞下每个操作”,例如null ,您可以使用NaN (非数字)而不是空值。

This

  1. would be easy to just transform every null into NaN.将每个空值转换为 NaN 很容易。

  2. any operation like x * y will give NaN if any operand is NaN, without any change to the code that performs computations.如果任何操作数是 NaN,任何像 x * y 这样的操作都将给出 NaN,而无需对执行计算的代码进行任何更改。

  3. If you really need nulls at the end then you can convert back NaNs to null.如果最后确实需要空值,则可以将 NaN 转换回空值。 (be careful, to test for a number to be NaN, use Math.IsNan() , never try to compare with === NaN as this will always give false, even NaN === NaN ) (小心,要测试一个数字是否为 NaN,请使用Math.IsNan() ,切勿尝试与=== NaN进行比较,因为这将始终给出 false,甚至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 .最后但并非最不重要的一点是,如果其中一个操作数是undefined ,则操作的结果将为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.所以实际上,你也可以把你的空值变成 undefined 而不是 NaN,从 C# null到 JS/TS undefined的转换可能更自然。 Might be easier on the serialization side as well (hint : Newtonsoft.Json.NullValueHandling.Ignore )在序列化方面也可能更容易(提示: 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.不确定这是否可以适应您的情况,但是如果您想避免像我上面写的那样使用条件使代码混乱,则值得检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM