小编Val*_*ori的帖子

如何内联检查空值并在打字稿中引发错误?

在 C# 中,我可以编写代码来检查空引用,以防抛出自定义异常,例如:

var myValue = (someObject?.SomeProperty ?? throw new Exception("...")).SomeProperty;
Run Code Online (Sandbox Code Playgroud)

在最近的更新中,TypeScript 引入了空合并运算符 ?? 但是像上面的语句一样使用它会产生编译错误。TypeScript 中是否有一些类似的允许语法?


澄清一下,所需的行为是通过以下代码实现的:

  if(someObject?.someProperty == null) {
    throw new Error("...");
  }

  var myValue = someObject.someProperty.someProperty;
Run Code Online (Sandbox Code Playgroud)

编码:

  var myValue = someObject?.someProperty.someProperty;
Run Code Online (Sandbox Code Playgroud)

在逻辑上正常工作,但抛出一个意义不大的异常。

syntax null null-check typescript

12
推荐指数
3
解决办法
3394
查看次数

如何在 QML 中操作颜色

我想做这样的事情:

Item {

    property color primary_color

    Rectangle {
        color: Qt.rgba(primary_color.red/2, primary_color.green, primary_color.blue, primary_color.alpha<0.5?0.25:0.75)
    }
}
Run Code Online (Sandbox Code Playgroud)

但我怎样才能访问这些属性呢?

qt colors rgba qml

2
推荐指数
1
解决办法
8149
查看次数

标签 统计

colors ×1

null ×1

null-check ×1

qml ×1

qt ×1

rgba ×1

syntax ×1

typescript ×1