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 - ?.

173 questions
276
votes
5 answers

How can I use optional chaining with arrays and functions?

I'm trying to use optional chaining with an array instead of an object but not sure how to do that: Here's what I'm trying to do myArray.filter(x => x.testKey === myTestKey)?[0]. Also trying similar thing with a function: let x = {a: () => {}, b:…
Black Mamba
  • 13,632
  • 6
  • 82
  • 105
152
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
  • 140,679
  • 96
  • 298
  • 425
84
votes
1 answer

Question mark before dot in javascript / react

I know what a ternary operator is in React. When I'm developing a React Native app I encounter this kind of syntax that is covered by my ESLint as an unexpected token: ESLint: Parsing error: Unexpected token . It goes like this: const routeName =…
Robert Tirta
  • 2,593
  • 3
  • 18
  • 37
51
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
  • 15,863
  • 5
  • 72
  • 116
39
votes
7 answers

How to chain attribute lookups that might return None in Python?

My problem is a general one, how to chain a series of attribute lookups when one of the intermediate ones might return None, but since I ran into this problem trying to use Beautiful Soup, I'm going to ask it in that context. Beautiful Soup parses…
David Hull
  • 1,255
  • 1
  • 14
  • 17
28
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
  • 53,704
  • 14
  • 91
  • 128
24
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
  • 26,790
  • 46
  • 179
  • 291
23
votes
1 answer

Object is possibly 'undefined' Error on Optional Chaining Typescript

I have a response which I am accessing: data?.currentOrganization?.onboardingSteps?. As you can guess, data, currentOrganization, and onboardingSteps might all be null. I want to assign a variable as follows: const hasSteps =…
Caleb Robinson
  • 1,010
  • 13
  • 21
22
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
  • 4,111
  • 5
  • 35
  • 54
21
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
  • 14,540
  • 9
  • 54
  • 83
20
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
  • 272,448
  • 266
  • 850
  • 1,236
17
votes
8 answers

In JavaScript, is there an easier way to check if a property of a property exists?

Is there an easy way to natively determine if a deep property exists within an object in JavaScript? For example, I need to access a property like this: var myVal = appData.foo.bar.setting; But there is a chance that either foo, foo.bar, or…
A.J. Brown
  • 913
  • 7
  • 14
16
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
  • 8,804
  • 9
  • 59
  • 90
14
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…
12
votes
1 answer

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
  • 1,118
  • 2
  • 13
  • 22
1
2 3
11 12