6

In my .js files, when I use an elvis operator const something = data?.info?.set , the entire file turns red after the line used.

How can I make VSCode recognize elvis operators and maintain syntax proper highlighting?

Note: I use metro-react-native-babel-preset and eslint, which by default allows for optionals in pure .js files (not typescript)

I have installed https://marketplace.visualstudio.com/items?itemName=ms-vscode.vscode-typescript-next and also followed the instructions to enable it. However, my code is still pure red color starting the line after I use the ? operator.

TIMEX
  • 259,804
  • 351
  • 777
  • 1,080
  • 1
    Does this answer your question? [Is there a null-coalescing (Elvis) operator or safe navigation operator in javascript?](https://stackoverflow.com/questions/6613952/is-there-a-null-coalescing-elvis-operator-or-safe-navigation-operator-in-javas) – Igor Dec 10 '19 at 21:06
  • [The optional chaining operator proposal is at Stage 4 and thus will likely be in ECMAScript 2020](https://github.com/tc39/proposal-optional-chaining). – Heretic Monkey Dec 10 '19 at 21:23

4 Answers4

6

VS Code 1.41 supports syntax highlighting and IntelliSense for optional chaining (?.) out of the box.

If you still see red highlighting with VS Code 1.41, either:

Matt Bierner
  • 58,117
  • 21
  • 175
  • 206
1

My problem was solved by removing this plugin from VSCode :

enter image description here

Axel Paris
  • 303
  • 3
  • 7
0

EDIT: After OP's edit of the question, I tried to scour for some info. This is what I came up with:


I don't believe there's an Elvis operator in JS.

Instead, you can use the OR operator to return something specific without crashing it. So something like

const something = data.info.set || undefined;
tripathiakshit
  • 624
  • 5
  • 17
0

Disable or remove this extension completely from your vs code

The Extension causing the syntax error, hopefully, it works for you as it did for me.

Hargent
  • 1
  • 1
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/33805512) – Aaron Meese Feb 14 '23 at 18:32