在 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)
在逻辑上正常工作,但抛出一个意义不大的异常。
我想做这样的事情:
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)
但我怎样才能访问这些属性呢?