Reputation: 1699
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.
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
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:
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:
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