Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double double questionmarks in TypeScript

Tags:

typescript

I have found such kind of code:

const dealType = currentDealType ?? originalDealType ?? '';

What ?? ?? what does mean the syntax?

like image 658
Tomasz Waszczyk Avatar asked Jun 17 '20 09:06

Tomasz Waszczyk


People also ask

What does 2 question marks mean in TypeScript?

The double question marks (??) are also called nullish coalescing operators and they allow to use of a default value set on the right side of the operator in case the initial value from the left side of the operator is null or undefined .

What does 2 question marks mean in JavaScript?

The JavaScript double question mark (??) operator is called the nullish coalescing operator and it provides a default value when a variable or an expression evaluates to null or undefined.

What does question mark mean in TypeScript?

The question mark ? in typescript is used in two ways: To mention that a particular variable is optional. To pre-check if a member variable is present for an object.

What does double question mark mean react?

The double question mark operator is called the nullish coalescing operator, and it's a new feature of JavaScript ES2020 that allows you provide a default value to use when a variable expression evaluates to null or undefined .

What is a double question mark in JavaScript?

The JavaScript double question mark is also known as the nullish coalescing operator. It’s an operator that simply returns the right-side expression when the left side expression is either null or undefined.

What does the question mark mean in typescript?

What does ?: mean in TypeScript? Using a question mark followed by a colon ( ?:) means a property is optional. That said, a property can either have a value based on the type defined or its value can be undefined. Another way to think about using a question mark with a colon (?:) is to define two types to a property where one of them is undefined.

What is the difference between as question mark and optional parameter?

As question mark marks the variable as optional, but any optional parameter should follow with required parameter. Function here requires any parameter to test if null or undefined. One of the most important point which has been resolved with TypeScript 3.7 version is continuously checking of variables or expressions for being null or undefined

Does typescript check for null or undefined?

One of the most important point which has been resolved with TypeScript 3.7 version is continuously checking of variables or expressions for being null or undefined Let us consider few examples below to know much better on the implementation and the use of TypeScript question mark.


1 Answers

It's the nullish coalescing operator that has been proposed for ecmascript and has been implemented in Typescript. You can read more here or here

The gist of it is that

const dealType = currentDealType ?? originalDealType;

is equivalent to:

const dealType = currentDealType !== null && currentDealType !== void 0 ? currentDealType : originalDealType;

Or in words: if currentDealType is null or undefined use originalDealType otherwise use currentDealType

like image 122
Titian Cernicova-Dragomir Avatar answered Nov 14 '22 23:11

Titian Cernicova-Dragomir