Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optional chaining operator support in VSCode

React Native 0.56 provides native support of Optional Chaining Operator i.e ?.

However the latest stable release of VS Code can not recognize this syntax and throw a TypeScript validation error:

[ts] Expression expected.

while no complie-time or eslint error was thrown.

How can I do to fix it?


Update on 2019/12/11:

Optional chaining is offically supported by TypeScript since 3.7! 😍

If you still meet this error, probably the TypeScript lib shipped with your VSCode is < 3.7.

You can fix it simply by installing the latest version of typescript (>= 3.7) to your workspace. (by npm or yarn as dev deps)

Then open the command palette, type tstv, and switch the TypeScript version used for JS and TS features from VS Code's Version to Workspace's Version (make sure it's >= 3.7) and voila!

This is definitely an awesome feature, thanks your all and happy hacking!

like image 497
kirkcola Avatar asked Aug 08 '18 07:08

kirkcola


People also ask

How do I enable optional chaining?

To enable optional chaining, you need to install a package. At the time of writing, optional chaining is not natively supported in Javascript, it is a new feature introduced in ES2020. Until it is fully adopted we can get all the optional goodness by installing a package!

What is optional chaining operator?

The optional chaining operator ( ?. ) enables you to read the value of a property located deep within a chain of connected objects without having to check that each reference in the chain is valid.

What is optional chaining operator TypeScript?

In TypeScript, optional chaining is defined as the ability to immediately stop running an expression if a part of it evaluates to either null or undefined .

When was optional chaining added to Javascript?

Optional chaining was introduced in ES2020. According to TC39 it is currently at stage 4 of the proposal process and is prepared for inclusion in the final ECMAScript standard.


Video Answer


2 Answers

VS Code 1.41 supports optional chaining in both JavaScript and TypeScript files. This support includes syntax highlighting and IntelliSense.

If you are using VS Code 1.41+ and optional chaining is not working as expected, try:

  • Check your installed extensions. Some of them may not yet understand optional chaining, which could cause errors or bad syntax highlighting

  • If you are using a workspace typescript version, make sure it is TypeScript 3.7+

like image 56
Matt Bierner Avatar answered Sep 19 '22 08:09

Matt Bierner


You can install JavaScript and TypeScript Nightly, then reopen VSCode.

like image 41
icbbetter Avatar answered Sep 17 '22 08:09

icbbetter