This question already has an answer here:
这个问题已经有了答案:
- Typescript the safe navigation operator ( ?. ) or (!.) and null property paths 3 answers
- 安全导航操作符(?)或者(!)和空属性路径3的答案。
- Replacement of Elvis Operator of Angular2 in Typescript 1 answer
- 替换打字稿1答案中Angular2的Elvis运算符
We have the Null coalescing operator in .NET
and we can use as below
我们在。net中有空合并运算符,我们可以如下所示
string postal_code = address?.postal_code;
Same thing can we do in React JS?
在response JS中我们可以做同样的事情?
What i found like we can do with && operator
我发现我们可以用&&操作员
in address.ts file
在地址。ts文件
string postal_code = address && address.postal_code;
what i need like .net feature is possible in typescript with react JS, is that possible ?
我需要的。net特性在带有response JS的打字稿中是可能的,这可能吗?
something like:
喜欢的东西:
string postal_code = address?.postal_code // I am getting the error in this line if I try to use like .NET
2 个解决方案
#1
3
This is a proposed feature in TypeScript, under the legendary Issue #16
这是在传奇性的第16期《打字稿》中提出的特性
It won't be introduced into TypeScript until the ECMAScript spec for this feature is firm as there is a desire for the TypeScript implementation to follow that specification - so you'll get it early, but not massively early in this case.
它不会被引入到TypeScript中,直到该特性的ECMAScript规范是严格的,因为有一种对TypeScript实现的渴望来遵循这个规范——所以您将会尽早得到它,但在这种情况下不会大量出现。
It is referred to as any of the following:
它被称为以下任何一种:
- Null Propagation Operator
- 零传播算子
- Existential Operator
- 存在操作符
- Null Coalesce Operator
- 零合并操作符
#2
0
JavaScript doesn't have a null-coalescing operator (nor does TypeScript, which mostly limits itself to adding a type layer and adopting features that are reasonably far along the path to making it into JavaScript). There is a proposal for a JavaScript null
-coalescing operator, but it's only at Stage 1 of the process.
JavaScript没有一个空合并操作符(也不包括TypeScript,它主要限制自己添加一个类型层,并采用在使其成为JavaScript的路径上合理的特性)。有人提议使用JavaScript的null-coalescing运算符,但它还只是该过程的第一步。
Using the &&
idiom you've described is a fairly common approach:
使用你所描述的&&习语是一种相当常见的方法:
let postal_code = address && address.postal_code;
If address
is null
(or any other falsy¹ value), postal_code
will be that same value; otherwise, it will be whatever value address.postal_code
was.
如果地址是空(或任何其他falsy¹值),postal_code将同样的价值;否则,它将是任何值地址。postal_code。
¹ The falsy values are 0
, ""
, NaN
, null
, undefined
, and of course false
.
¹falsy值0”“南,null,未定义,当然假的。
#1
3
This is a proposed feature in TypeScript, under the legendary Issue #16
这是在传奇性的第16期《打字稿》中提出的特性
It won't be introduced into TypeScript until the ECMAScript spec for this feature is firm as there is a desire for the TypeScript implementation to follow that specification - so you'll get it early, but not massively early in this case.
它不会被引入到TypeScript中,直到该特性的ECMAScript规范是严格的,因为有一种对TypeScript实现的渴望来遵循这个规范——所以您将会尽早得到它,但在这种情况下不会大量出现。
It is referred to as any of the following:
它被称为以下任何一种:
- Null Propagation Operator
- 零传播算子
- Existential Operator
- 存在操作符
- Null Coalesce Operator
- 零合并操作符
#2
0
JavaScript doesn't have a null-coalescing operator (nor does TypeScript, which mostly limits itself to adding a type layer and adopting features that are reasonably far along the path to making it into JavaScript). There is a proposal for a JavaScript null
-coalescing operator, but it's only at Stage 1 of the process.
JavaScript没有一个空合并操作符(也不包括TypeScript,它主要限制自己添加一个类型层,并采用在使其成为JavaScript的路径上合理的特性)。有人提议使用JavaScript的null-coalescing运算符,但它还只是该过程的第一步。
Using the &&
idiom you've described is a fairly common approach:
使用你所描述的&&习语是一种相当常见的方法:
let postal_code = address && address.postal_code;
If address
is null
(or any other falsy¹ value), postal_code
will be that same value; otherwise, it will be whatever value address.postal_code
was.
如果地址是空(或任何其他falsy¹值),postal_code将同样的价值;否则,它将是任何值地址。postal_code。
¹ The falsy values are 0
, ""
, NaN
, null
, undefined
, and of course false
.
¹falsy值0”“南,null,未定义,当然假的。