【问题标题】:Double double questionmarks in TypeScriptTypeScript 中的双双问号
【发布时间】:2020-06-17 09:55:17
【问题描述】:

我找到了这样的代码:

const dealType = currentDealType ?? originalDealType ?? '';

?? ??的语法是什么意思?

【问题讨论】:

  • 不是特殊运算符,是同一个运算符应用了两次。和1 + 2 + 3一样,是+使用了两次而不是+ ... +
  • 它是?? 使用了两次 :-)

标签: typescript


【解决方案1】:

这是为 ecmascript 提出并已在 Typescript 中实现的空值合并运算符。你可以阅读更多herehere

它的要点是

const dealType = currentDealType ?? originalDealType;

相当于:

const dealType = currentDealType !== null && currentDealType !== void 0 ? currentDealType : originalDealType;

或者用文字表示:如果currentDealTypenullundefined,则使用originalDealType,否则使用currentDealType

【讨论】:

  • 谢谢!在 9 分钟内,我会将答案标记为“接受”。谢谢,祝您有愉快的一天!
猜你喜欢
  • 2012-12-06
  • 2021-11-02
  • 2021-07-23
  • 1970-01-01
  • 2017-08-23
  • 1970-01-01
  • 2022-11-17
  • 1970-01-01
相关资源
最近更新 更多