Typescript equivalent of elvis “?: return” operator

Viewed 186

The following code in Kotlin return out of the parent function if the result of nextbitmap() is null

 val bitmap = nextbitmap() ?: return
 // something the relies on bitmap not being null

What is the Typescript equivalent of such operator ?

1 Answers

There is no such operator, and return can never be part of an expression. In TypeScript, you'll have to use a separate if statement for this:

const bitmap = nextbitmap();
if (!bitmap) return;
// something the relies on bitmap not being null

(or use bitmap != null in the condition, if bitmap has a primitive type)

Related