typescript VSCode 中的可选链运算符支持

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51741333/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 05:34:07  来源:igfitidea点击:

Optional chaining operator support in VSCode

typescriptvisual-studio-codebabeljs

提问by kirkcola

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

React Native 0.56 提供了对Optional Chaining Operatorie 的原生支持?.

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

然而,最新的稳定版 VS Code 无法识别此语法并引发 TypeScript 验证错误:

[ts] Expression expected.

[ts] Expression expected.

while no complie-time or eslint error was thrown.

虽然没有抛出编译时或 eslint 错误。

How can I do to fix it?

我该怎么做才能解决它?



Update on 2019/12/11:

2019/12/11 更新:

Optional chaining is offically supported by TypeScript since 3.7!

从 3.7 开始,TypeScript 正式支持可选链!

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

如果您仍然遇到此错误,则可能是您的 VSCode 附带的 TypeScript 库 < 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)

您只需将最新版本的打字稿(> = 3.7)安装到您的工作区即可修复它。(通过 npm 或 yarn 作为 dev deps)

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

然后打开命令面板,输入tstv,并将用于 JS 和 TS 功能TypeScript 版本VS Code 的版本切换到Workspace 的版本(确保它 >= 3.7),瞧!

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

这绝对是一个很棒的功能,谢谢大家,祝你黑客攻击愉快!

采纳答案by Matt Bierner

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

VS Code 1.41支持JavaScript 和 TypeScript 文件中的可选链接。此支持包括语法突出显示和 IntelliSense。

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

如果您使用的是 VS Code 1.41+ 并且可选链接没有按预期工作,请尝试:

  • 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+

回答by icbbetter

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

您可以安装JavaScript 和 TypeScript Nightly,然后重新打开 VSCode。

回答by DarkCrazy

This was still happening to me until I uninstalled the extension JavaScript and TypeScript IntelliSense

这仍然发生在我身上,直到我卸载了扩展JavaScript 和 TypeScript IntelliSense

So yes, check uninstalling old plugins if you are still having this issue. Also you can try the vscode insidersversion and check if this works in that one. It worked for me without doing any change.

所以是的,如果您仍然遇到此问题,请检查卸载旧插件。您也可以尝试使用vscode 内部人员版本并检查这是否适用于该版本。它对我有用,没有做任何改变。

回答by yotke

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

我刚刚解决了在 vscode json 设置中禁用 js/ts 验证的问题:

"javascript.validate.enable": false

"javascript.validate.enable": false

You my need to install eslint-plugin-babelfor eslint rules.

你需要安装eslint-plugin-babeleslint 规则。

{
  "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
  }
}