Questions tagged [nullish-coalescing]

34 questions
1
vote
1 answer

Destructuring possible null values within an array

Is there a better way to handle null values within the example below. In entries 5, 7, and 10; a null value is present. The following errors are presented whether the value is null or undefined. #5 - Uncaught TypeError: Cannot read property 'id' of…
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
1
vote
1 answer

Idiomatic way to test for non-nullishness

JavaScript now provides ?? and ?. to do things like find the first non-nullish expression or dereference an object if not null. [Added: "nullish" means "null or undefined", like the nullish coalescing operator]. Is there a good idiomatic way to…
Steve Bennett
  • 114,604
  • 39
  • 168
  • 219
0
votes
0 answers

null check / optional chaining for array contents?

we're all familiar with the tune?.image type of null checks in Typescript optional chaining docs but what's the best practice for checking this with an array value? tune.images?[0] will not work. I often use: tune.images ? tune.images[0] :…
dcsan
  • 11,333
  • 15
  • 77
  • 118
0
votes
0 answers

Type 'undefined' is not assignable to type ConnectionTimeoutError | XyzError while using nullish coalescing operator

export class BaseError { //rest of code static for = (error: AxiosError) => NetworkError.for(error) ?? Xyz.for(error); } Class static side 'typeof NetworkError' incorrectly extends base class static side 'typeof BaseError'. Type…
Mentalist
  • 35
  • 1
  • 11
0
votes
1 answer

Why is my nullish-coalascing operators not transpiled by the rollup plugin for babel?

I have a svelte project that I would like to transpile nullish-coalascing operators (es2020) to es5 using the rollup plugin @rollup/plugin-babel which is enabled by default in @babel/preset-env since babel v7.6.3. However in my project it does not…
cascading-jox
  • 982
  • 1
  • 7
  • 21
0
votes
1 answer

Is this an appropriate use of chained nullish operators?

the docs say that the nullish operator (??) is the 5th lowest precedence in operation. I am trying to use them in this manner const accountNumber = optionsA?.accountNumber ?? optionB?.accountNo ?? '7202705382'; which I hope to mean assign…
Kim Gentes
  • 1,496
  • 1
  • 18
  • 38
0
votes
0 answers

unexpected syntax after working with such syntax for sometimes now nullish coalescing in react.js

Failed to compile. Error in ./src/View.js Syntax error: C:/workspace/reactsapp/my-shop/src/View.js: Unexpected token (77:69) 75 | const [mainImage, setMainImage] = useState(product); 76 | const [orderQuantity, setQuantity] =…
Codad5
  • 1
  • 2
0
votes
1 answer

why does nullish coalescing executing two parts?

I have a method: const objT2 = { calcAge(year) { console.log(2022 - year); }, }; but when I use the nullish coalescing both parts is being executed. objT2.calcAge(1990) ?? console.log(`method not found`); //output => 32 method not found
0
votes
0 answers

Why JavaScript forbids combining Nullish operator (??) with And (&&), OR (||) operators?

Why JavaScript forbids it (gives syntax error), if Nullish operator (??) is combined with And (&&), OR (||) operators "without parenthesis"? Example: let x = 1 && 2 ?? 3; // Syntax error But following works let x = (1 && 2) ?? 3; //…
Sourabh Saxena
  • 127
  • 1
  • 1
  • 7
0
votes
2 answers

Both the sides of nullish coalescing operator in JavaScript are being executed

console.log(restaurant.orderPizza?.('onion','tomato','basil') ?? 'Method does not exist'); console.log(restaurant.orderRissotto?.('onion','tomato','basil') ?? 'Method does not exist' orderPizza and orderRissotto are the two methods inside object…
Linus
  • 39
  • 4
0
votes
1 answer

combination of OPTIONAL CHAINING and NULLISH COALESCING operator not rendering the expected result

i am just learning about this combo of Optional chain and Nullish coalescing. Here is the object const restaurant = { name_: 'Classico Italiano', location: 'Via Angelo Tavanti 23, Firenze, Italy', categories: ['Italian', 'Pizzeria', 'Vegetarian',…
user13899380
0
votes
0 answers

Due to Nullish Coalescing and Optional Chaining operators, on VS Code's terminal appear warnings

When I implement any of the aforementioned operators in code, there are appear warnings. But code runs without any problem though. My question is, what would be able to cause such a weirdness? PS: VScode version: 1.53.2
0
votes
1 answer

Destructuring an optionally chained object, to get the ...rest?

I have this setup const { discard_me, ...rest } = some?.optional?.chaining; I end up with an error that discard_me doesn't exist, but that's expected if chaining is also non-existent. It seems like the optional chaining should cover the concerns of…
AncientSwordRage
  • 7,086
  • 19
  • 90
  • 173
0
votes
1 answer

Question about Optional chaining and Nullish coalescing JavaScript operators support in PhpStorm

I wanted to ask if anyone knows how one can support the JavaScript nullish coalescing operator (??) and the optional chaining operator (?.) in PhpStorm. Currently I am developing a project in react-native in PhpStorm, both operators work fine but…
0
votes
1 answer

Is there sugar with Optional chaining/nullish coalescing to prevent Typeerrors with say Array.map?

Is there any sugar to ensure that map will not typeerror using tools like optional chaining/nullishcoalescing? let x = {y: 1, z: 2}; x?.map(i => i); // Typeerror Array.isArray(x)?.map(i => i); // Typeerror let y = '1234'; y?.length && y.map(i =>…
Armeen Moon
  • 18,061
  • 35
  • 120
  • 233