Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript equivalent of elvis “?: return” operator

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 ?

like image 221
TSR Avatar asked Feb 23 '26 21:02

TSR


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)

like image 60
Bergi Avatar answered Feb 26 '26 23:02

Bergi