Optional chaining operator support in VSCode

可紊 提交于 2019-12-17 23:09:07

问题


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!


回答1:


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+




回答2:


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




回答3:


I just resolved the issue with disabling js/ts validation in vscode json settings:

"javascript.validate.enable": false

You my need to install eslint-plugin-babel for eslint rules.

{
  "plugins": [
    "babel"
  ],
  "rules": {
    "babel/new-cap": 1,
    "babel/camelcase": 1,
    "babel/no-invalid-this": 1,
    "babel/object-curly-spacing": 1,
    "babel/quotes": 1,
    "babel/semi": 1,
    "babel/no-unused-expressions": 1,
    "babel/valid-typeof": 1
  }
}


来源:https://stackoverflow.com/questions/51741333/optional-chaining-operator-support-in-vscode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!