33

In javascript, Optional Chaining Operator is supported by the babel plugin.

But I can't find how to do this in Typescript. Any idea?

3
  • TypeScript does not support the optional chaining operator Commented Aug 23, 2017 at 15:30
  • 2
    (((some || {}).variable || {}).access || {}).nested Commented Aug 23, 2017 at 15:34
  • optional chaining is in stage 2 now! so it is more likely that it will be added to javascript and eventually typescript. Commented Jun 16, 2019 at 4:44

3 Answers 3

20

At time of writing, TypeScript does not support the optional chaining operator. See discussion on the TypeScript issue tracker: https://github.com/Microsoft/TypeScript/issues/16

As a warning, the semantics of this operator are still very much in flux, which is why TypeScript hasn't added it yet. Code written today against the Babel plugin may change behavior in the future without warning, leading to difficult bugs. I generally recommend people to not start using syntax whose behavior hasn't been well-defined yet.

Sign up to request clarification or add additional context in comments.

8 Comments

I'm sorry but I'm genuinely curious, how can the syntax of something like "user.address?.street" ever really change at this point? You have a question-mark before the period to check if address is not null before you check for a street. Sure maybe some behind the scenes code might change, but for a user implementing this -what could go wrong? No offense I'm just really curious
any news, one year later?
The TC39 committee is still arguing about syntax and behavior
I'll just mention here in case you are interested — there are a few utility functions that try to offer a typesafe alternative now, like typesafe-get, optional-chain and (my) get-optional. None of them is the same as a natively supported operator, of course, but they could be helpful anyway.
You have to consider the case where you say "Oh, all our runtimes support all the syntactic features we were using, so we can turn off downleveling" and you end up getting different behavior
|
12

Update Oct 15, 2019

Support now exists in [email protected]

Say thanks to https://stackoverflow.com/a/58221278/6502003 for the update!


Although TypeScript and the community are in favor of this operator, until TC39 solidifies the current proposal (which at the time of this writing is at stage 1) we will have to use alternatives.

There is one alternative which gets close to optional chaining without sacrificing dev tooling: https://github.com/rimeto/ts-optchain

This article chronicles what the creators were able to achieve in trying to mirror the native chaining operator:

  1. Use a syntax that closely mirrors chained property access
  2. Offer a concise expression of a default value when traversal fails
  3. Enable IDE code-completion tools and compile-time path validation

In practice it looks like this:

import { oc } from 'ts-optchain';

// Each of the following pairs are equivalent in result.
oc(x).a();
x && x.a;

oc(x).b.d('Default');
x && x.b && x.b.d || 'Default';

oc(x).c[100].u.v(1234);
x && x.c && x.c[100] && x.c[100].u && x.c[100].u.v || 1234;

Keep in mind that alternatives like this one will likely be unnecessary once the proposal is adopted by TypeScript.

Also, a big thanks to Ryan Cavanaugh for all the work you are doing in advocating this operator to TC39!

1 Comment

As a bonus if you'd like to fix VS Code linting which uses typescript, point to a typescript v >= 3.7.0-beta in settings.json with "typescript.tsdk": "${npm global dir}/lib/node_modules/typescript/lib"
6

Typescript 3.7 beta has now support for Optional chaining 👍🎉

You can now write code like this:

let x = foo?.bar?.baz;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.