相当于 elvis “?: return” 运算符的 typescript

标签 typescript null

如果 nextbitmap() 的结果为 null,则 Kotlin 中的以下代码从父函数返回

 val bitmap = nextbitmap() ?: return
 // something the relies on bitmap not being null
这种运算符的 Typescript 等价物是什么?

最佳答案

没有这样的操作符,而且return永远不能成为表达式的一部分。在 TypeScript 中,您必须使用单独的 if声明:

const bitmap = nextbitmap();
if (!bitmap) return;
// something the relies on bitmap not being null
(或在条件中使用 bitmap != null,如果 bitmap 具有原始类型)

关于相当于 elvis “?: return” 运算符的 typescript ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64083222/

相关文章:

typescript - 在 Typescript 中优雅地将两种类型组合成一个接口(interface)

typescript - Vuejs Typescript 类组件 refs.focus 不工作

r - 如何编写一个将 NULL 传递给 ggplot 而不会出现美学错误的函数

c++ - 尽管有切片,为什么 static_cast 仍然可以使用空指针?

c - 释放后如何将arraylist设置为空?

java - JTextField.getText() 返回 null 值,即使它里面有内容

typescript - Angular2 返回一个已经解决的 promise

typescript - 从 firebase 检索用户配置文件数据并显示

javascript - 如何使用 Angular 8 单元测试检查图像是否存在于给定路径中?

null - 如何检查变体是否为空?