developer
developer

Reputation: 1699

Optional chaining operator in Visual Studio Code

I am trying to use optional chaining operator (?) in TypeScript 4.1.2.

handleError(err: any) {
    let message = err.error?.message;
}

It doesn't give any compilations error however visual studio code shows "Expression expected" error.

VS Code Error

How can I fix this?

package.json

{
  "name": "app",
  "version": "0.0.0",
  "scripts": {
  },
  "private": true,
  "dependencies": {
    "typescript": "~4.1.2"
  }
}

Upvotes: 0

Views: 328

Answers (1)

Lauren Yim
Lauren Yim

Reputation: 14148

Make sure that VS Code is using the project's version of TypeScript installed in node_modules.

From the documentation on TypeScript in VS Code:

The active TypeScript version and its install location are displayed in the Status Bar when viewing a TypeScript file:

TypeScript status bar version

Using the workspace version of TypeScript

If your workspace has a specific TypeScript version, you can switch between the workspace version of TypeScript and the version that VS Code uses by default by opening a TypeScript or JavaScript file and clicking on the TypeScript version number in the Status Bar. A message box will appear asking you which version of TypeScript VS Code should use:

TypeScript version selector

Use this to switch between the version of TypeScript that comes with VS Code and the version of TypeScript in your workspace. You can also trigger the TypeScript version selector with the TypeScript: Select TypeScript Version command.

Upvotes: 1

Related Questions