Questions tagged [optional-chaining]

Optional chaining is a process for querying and calling properties, methods, and subscripts on an optional that might currently be nil/null . If the optional contains a value, the property, method, or subscript call succeeds; if the optional is nil/null , the property, method, or subscript call returns nil/null. Use for questions about such chaining with a appropriate language tag like [swift], [JavaScript], etc.

Optional chaining is a process for querying and calling properties, methods, and subscripts on an optional that might currently be nil/null . If the optional contains a value, the property, method, or subscript call succeeds; if the optional is nil/null , the property, method, or subscript call returns nil/null.

JavaScript Optional chaining operator - ?.

84 questions
52
votes
2 answers

Using optional chaining operator for object property access

TypeScript 3.7 now supports the optional chaining operator. Hence, you can write code such as: const value = a?.b?.c; I.e., you can use this operator to access properties of an object, where the object itself may be null or undefined. Now what I…
Golo Roden
  • 112,924
  • 78
  • 260
  • 376
21
votes
2 answers

How can I combine destructuring assignment and optional chaining?

I have a TypeScript interface with some optional fields and a variable of that type: interface Foo { config?: { longFieldName?: string; } } declare let f: Foo; I'd like to put longFieldName in a variable of the same name. If config…
danvk
  • 13,227
  • 3
  • 51
  • 86
19
votes
2 answers

TypeScript 3.7.2 - Support for the experimental syntax 'optionalChaining' isn't currently enabled

In my project I am using TS 3.7.2 which should support optional chaining. But when I try to use it like that: const state = urlParams.state?.toString() I get the error: Support for the experimental syntax 'optionalChaining' isn't currently…
flppv
  • 3,061
  • 3
  • 23
  • 41
18
votes
8 answers

Optional Chaining in JavaScript

I've been programming a lot in Swift recently. Today I did some work in JavaScipt when question popped up to me: Is there something similar to optional chaining in JavaScript? A way to prevent undefined is not an object without any…
idmean
  • 13,418
  • 7
  • 47
  • 78
16
votes
3 answers

How to make webpack accept optional chaining without babel

Scenario: We're using webpack 4 to create a bundle from our Javascript sources. We're not using Babel because we're authoring only for a single platform (latest Chrome), and we're only using features directly available in Chrome, thus no…
connexo
  • 41,035
  • 12
  • 60
  • 87
15
votes
2 answers

Optional Chaining - Function.prototype.apply was called on undefined, which is an undefined and not a function

Using optional chaining with function calls causes the expression to automatically return undefined instead of throwing an exception if the method isn't found. Note: The code is using spread syntax, not rest parameters. const fn1 = undefined const…
Wenfang Du
  • 1,884
  • 3
  • 14
  • 33
14
votes
1 answer

JavaScript optional chaining dynamic property

I am trying to access a dynamic property with safety provided by optional chaining that is available in TS. However it appears that this is not valid. export const theme = { headers: { h1: { }, h6: { color: '#828286' }, …
Jamie Hutber
  • 22,870
  • 34
  • 131
  • 236
13
votes
2 answers

How to get optional chaining working in TypeScript?

Looks like optional chaining has landed. Here's an example What I can't figure out is how to get TS to compile it properly. I'm not getting any syntax errors in my project, but this: let imageFileId = (await db.query(sql`select id from image_files…
mpen
  • 237,624
  • 230
  • 766
  • 1,119
9
votes
2 answers

Expo Web failed to compile "optional chaining"

Looks like Expo Webpack doesn't optional chaining. I found this when I tried to install UI Kitten to Expo Web app. This is the compile error after I added UI Kitten to the newly created Expo…
7
votes
3 answers

Optional Chaining not enabled ReactNative

I am getting this error when running the android project in react react native. This is fresh install of react native version "react": "^16.3.1","react-native": "^0.57.1", It gives error of optional chaining. Can anyone please help me how to enable…
Vinay Sikarwar
  • 411
  • 6
  • 18
5
votes
1 answer

How are these two lines of code different that one throws an error and the other doesn’t?

console.log("#1", "a12312a".match(/^\d+/)?.[0].length); console.log("#2", ("a12312a".match(/^\d+/)?.[0]).length); I was writing some code and stumbled upon something I don’t understand. In Chrome 89.0.4389.128 (Official Build) (64-bit), the…
5
votes
2 answers

Prettier: optional chaining support

Vs code > OUTPUT > Prettier 2/23/2020, 12:10:36 PM: ----------------------- Expression expected. (/Users/yanivper/dev/test/test.ts:2:23) 1 | function test(obj) { 2 | return obj.a?.b; ^ In a new project: npm i…
Yaniv Peretz
  • 734
  • 7
  • 21
5
votes
3 answers

TS1109, WebStorm incorrectly reacting on optional chaining

WebStorm thinks it is a ternary operator and expects expressions and colon next. Maybe it thinks it is a TS code, while this is JS code in .js file, I'm not sure. How can I switch this inspection off?
Winni
  • 75
  • 6
5
votes
4 answers

Optional chaining used in left side of assignment in Swift

What does it mean when optional chaining is used in the left side of the assignment statement? Will the app crash if the optional variable is nil? e.g. // cell is a UITableViewCell cell.textLabel?.text = "Test"
Boon
  • 37,606
  • 51
  • 186
  • 296
4
votes
0 answers

@babel/plugin-proposal-optional-chaining not working inside Vue.js